python - How to convert a csv file, that has array data, into a json file? -
i have csv file bunch of columns. few of columns same, want convert them json object live under same array.
for example in csv:
firstname,lastname,pet,pet,pet joe, dimaggio, dog, cat pete, rose, turtle, cat jackie, robinson, dog i want json be
{ firstname: joe, lastname: dimaggio, pets: ["dog", "cat"] }, { firstname: pete, lastname: rose, pets: ["turtle", "cat"] }, { firstname: jackie, lastname: robinson, pets: ["dog"] } i'm trying write simple python script i'm running problems.
here's i've got far:
import csv import json csvfile = open('userdata.csv', 'r') jsonfile = open('userdata.json', 'w') fieldnames = ("firstname", "lastname", "pet", "pet", "pet"); reader = csv.dictreader( csvfile, fieldnames) record = {} row in reader: record['firstname'] = row['firstname'] record['lastname'] = row['lastname'] record['pets'] = json.jsonencoder().encode({"pets": [row['pet'], row['pet'], row['pet'], row['pet'], row['pet']]}); json.dump(record, jsonfile, indent=4) ##json.dump(json.loads(json.jsonencoder(record)), jsonfile, indent=4) print "something worked" but acting funny since it's printing pets array inside object called pets.
i can't figure out how array pets outside object `pets. it's adding backslashes array items
{ "firstname": "joe", "lastname": "dimaggio", "pets": "{\"pets\": [\"dog\", \"cat\"]}" }
it because encoding , using json.dumps encoding twice. remove json.jsonencoder().encode(...) , should work correctly.
import csv import json csvfile = open('userdata.csv', 'r') jsonfile = open('userdata.json', 'w') fieldnames = ("firstname", "lastname", "pet", "pet", "pet"); reader = csv.dictreader( csvfile, fieldnames) record = {} row in reader: record['firstname'] = row['firstname'] record['lastname'] = row['lastname'] record['pets'] = [[row['pet'], row['pet'], row['pet'], row['pet'], row['pet']] # remove blank entries record['pets'] = [x x in record['pets'] if x not ''] json.dumps(record, jsonfile, indent=4) print "something worked" the backslashes saw escaping json string, result of serializing twice.
Comments
Post a Comment