jeri teri
jeri teri

Reputation: 65

Python Json to csv, Extract the specified keys,KeyError:

I need to convert json to csv, I just want to extract some keys in the file, but some keys do not exist in the json file, I hope it can automatically fill in these non-existent keys


import csv
import json
import sys
import codecs

def trans(path):
    jsonData = codecs.open('C:/Users/jeri/Desktop/1.json', 'r', 'utf-8')
    # csvfile = open(path + '.csv', 'w') 
    # csvfile = open(path + '.csv', 'wb') 
    csvfile = open('C:/Users/jeri/Desktop/1.csv', 'w', newline='', encoding='utf-8')  
    writer = csv.writer(csvfile, delimiter=',')
    keys = ['dob','firstname','lastname']
    writer.writerow(keys)

    for line in jsonData:
        dic = json.loads(line)
        writer.writerow([dic['dob'],dic['firstname'],dic['lastname'],])

    jsonData.close()
    csvfile.close()

if __name__ == '__main__':
    path = str(sys.argv[0])
    print(path)
    trans(path)

Console prompt::

Traceback (most recent call last):
  File "C:\Users\jeri\PycharmProjects\pythonProject9\main.py", line 25, in <module>
    trans(path)
  File "C:\Users\jeri\PycharmProjects\pythonProject9\main.py", line 17, in trans
    writer.writerow([dic['dob'],dic['firstname'],dic['lastname'],])
KeyError: 'dob'

Upvotes: 0

Views: 311

Answers (3)

BoarGules
BoarGules

Reputation: 16942

If the key 'dob' might be missing, instead of dic['dob'], do dic.get('dob', None). That provides the default you want.

Upvotes: 1

lakshmi narayana
lakshmi narayana

Reputation: 84

you can transform your for loop into something like this.

for line in jsonData:
    dic = json.loads(line)
    dob = dic['dob'] if "dob" in dic else None
    firstname = dic['firstname'] if "firstname" in dic else None
    lastname = dic['lastname'] if "lastname" in dic else None
    writer.writerow([dob,firstname,lastname])

Upvotes: 0

Mahmoud Embaby
Mahmoud Embaby

Reputation: 317

I think this would solve your problem. (I defined a function to test the existence of each item in json, if exists it return the value and if it doesn't exists it returns 'N/A')

def getValue(dic, item):
    try:
        return dic[item]
    except:
        return 'N/A'

for line in jsonData:
    dic = json.loads(line)
    writer.writerow([getValue(dic, 'dob'), getValue(dic, 'firstname'), getValue(dic, 'lastname'),])

Upvotes: 1

Related Questions