Home > Blockchain >  Write a Python dictionary into csv file after a for loop
Write a Python dictionary into csv file after a for loop

Time:10-08

The code below is meant to request nested json datasets from an online database and extract only the information I need. I can print the results, so it basically works. However, I don't know how to write these results on a .csv file. I'm afraid I cannot provide a Minimal Working Example, as this would imply revealing my login and password. However any help on how I could do this would be most welcome, as I am a beginner.


# returns JSON object as a dictionary
json_complete = json.load(f)

# retrieves specific data
for i in json_complete['data']:
    # documentId=i['documentId']
    if i['topics'] != None:
        for x in i['topics']:
            documentId=x['documentId']
            placeCited=x['topicPlaceName']
            year=i['date']['docYear']
            month=i['date']['docMonth']
            day=i['date']['docDay']
            documentDate=str(year)  "-"   str(month) "-"   str(day)
            
            print(str(documentId)   ','   placeCited   ','  documentDate   ',') 

this is a snippet of the output I get:

57217,Iran / Asia / World / Top of the TGN hierarchy,1561-7-26,
57217,Halab / Halab / Suriyah / Asia,1561-7-26,
57224,Istanbul / Istanbul / Marmara / Turkiye,1561-8-8,
57224,Iran / Asia / World / Top of the TGN hierarchy,1561-8-8,
57224,Halab / Halab / Suriyah / Asia,1561-8-8, 

I am hoping to get a csv with header along those lines:

'DocumentID', 'Place', 'Date'

CodePudding user response:

Try this.

json_complete = json.load(f)

with open("test.csv") as file:
    file.write('DocumentID, Place, Date\n') # write the header from in file.
# retrieves specific data
    for i in json_complete['data']:
        # documentId=i['documentId']
        if i['topics'] != None:
            for x in i['topics']:
                documentId=x['documentId']
                placeCited=x['topicPlaceName']
                year=i['date']['docYear']
                month=i['date']['docMonth']
                day=i['date']['docDay']
                documentDate=str(year)  "-"   str(month) "-"   str(day)
                
                file.write(str(documentId)   ','   placeCited   ','  documentDate '\n') # write the data row to file

CodePudding user response:

Use csv library. https://docs.python.org/3/library/csv.html

import csv
file = open("file.csv", 'w', newline = "")
csvwriter = csv.writer(file)
csvwriter.writerow(['DocumentID', 'Place', 'Date'])
for i in json_complete['data']:
# documentId=i['documentId']
if i['topics'] != None:
    for x in i['topics']:
        documentId=x['documentId']
        placeCited=x['topicPlaceName']
        year=i['date']['docYear']
        month=i['date']['docMonth']
        day=i['date']['docDay']
        documentDate=str(year)  "-"   str(month) "-"   str(day)
        csvwriter.writerow([documentId, placeCited, documentDate])
file.close()

This will do the work. Hope it helps.

  • Related