Python Forum
In CSV, how to write the header after writing the body?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In CSV, how to write the header after writing the body?
#18
(Jan-04-2018, 07:16 AM)Larz60+ Wrote: OK, I got it to work. This will make life a lot easier for you.
first the code, and a sample run. Explanation after that:

Thank you so much for your code. I leaned a lot from it. The idea of making dictionary for each .bib item and then export it as .json was brilliant. I've amended the code to generate both .csv file and .json file.
The new amended code is

import json
import csv
import os



class BIB2CSV:
    def __init__(self, CSVName, BIBName,JsonName):
        self.CSVName = CSVName
        self.BIBName = BIBName
        self.JsoName = JsonName
        self.BIBData = {}

    def make_dict(self):
        with open(self.BIBName, 'r', encoding="utf8") as BIBFilePointer:
            line1 = ''
            longline = False
            for line in BIBFilePointer:
                line = line.strip()
                if '@' not in line and not line.endswith('},') and not line.endswith('},}'):
                    line1 += line
                    longline = True
                    continue
                if longline:
                    longline = False
                    line = line1 + line
                    line1 = ''
                if line.startswith('@'):
                    EntryKey = line[line.index('{') + 1:]
                    EntryKey = EntryKey.replace(',', '')
                    self.BIBData[EntryKey] = {}
                else:
                    name = line[:line.index('=')]
                    value = line[line.index('=') + 2:line.index('}')]
                    self.BIBData[EntryKey][name] = value
        # Save it to json file for future use
        with open(self.JsoName, 'w') as jo:
            json.dump(self.BIBData, jo, indent=2)

    def CreateCSV(self):
        # pdb.set_trace()

        BIBKeys = [Key for Key in self.BIBData]
        CSVHeader = ["ItemKey"]
        for HeaderItem in self.BIBData[BIBKeys[1]]:
            CSVHeader.append(HeaderItem)


        with open('CSVTemp.csv', 'w', encoding='utf-8', newline='') as CSVFilePointer:
            CSVWriterPointer = csv.writer(CSVFilePointer)
            for BibKey in self.BIBData:
                CSVLineContent = []
                # This loop take an item from the CSV and put its value in the CSV line
                for HeaderItem in CSVHeader:
                    if HeaderItem == "ItemKey":
                        CSVLineContent.append(BibKey)
                        continue
                    if HeaderItem in self.BIBData[BibKey]:
                        CSVLineContent.append(self.BIBData[BibKey][HeaderItem])
                    else:
                        CSVLineContent.append('')

                # This loop search for a new head item an add it to the CSV head
                for HeaderItem in self.BIBData[BibKey]:
                    if HeaderItem not in CSVHeader:
                        CSVHeader.append(HeaderItem)
                        CSVLineContent.append(self.BIBData[BibKey][HeaderItem])

                CSVWriterPointer.writerow(CSVLineContent)

        with open('CSVTemp.csv') as CSVFilePointer:
            CSVReaderPointer = csv.reader(CSVFilePointer)
            with open(self.CSVName, 'w+', newline='') as CSVFilePointer2:
                CSVWriterPointer = csv.writer(CSVFilePointer2)
                CSVWriterPointer.writerow(CSVHeader)
                for Row in CSVReaderPointer:
                    CSVWriterPointer.writerow(Row)
        os.remove('CSVTemp.csv')



def main():
    CSVName ='CSVFile.csv'
    BIBName = 'ConferencePublications.bib'
    JsonName = 'JsonFile.json'
    BibConverter = BIB2CSV(CSVName, BIBName, JsonName)
    BibConverter.make_dict()
    BibConverter.CreateCSV()


if __name__ == '__main__':
    main()
GitHub is:
https://github.com/mTamim/Bib2CSV

Thank you so much Larz60+
Reply


Messages In This Thread
RE: In CSV, how to write the header after writing the body? - by Tim - Jan-06-2018, 01:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [BeautifulSoup] Find </body>? Winfried 3 1,440 Jul-21-2023, 11:25 AM
Last Post: Gaurav_Kumar
  Get html body of URL rama27 6 3,569 Aug-03-2020, 02:37 PM
Last Post: snippsat
  malformed header from script 'main.py': Bad header: * Serving Flask app "main" anuragsapanbharat 2 4,596 Jun-12-2019, 07:26 AM
Last Post: anuragsapanbharat
  Is it possible to perform a PUT request by passing a req body instead of an ID ary 0 1,867 Feb-20-2019, 05:55 AM
Last Post: ary

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020