Reputation: 8009
I want to write rows with uneven columns. Below is my code:
import csv
import json
path = 'E:/Thesis/thesis_get_data'
outputfile = open('maplight_113.csv', 'w', newline='')
outputwriter = csv.writer(outputfile)
with open (path + "/" + 'maplight data 113congress.json',"r") as f:
data = json.load(f)
for bill in data['bills']:
b = bill.get('measure')
for organization in bill['organizations']:
d = organization.get('name')
f = organization.get('disposition')
a = (organization.get('catcode'))
outputwriter.writerow([b, d, a, f])
outputfile.flush();
Every "bill.get('measure')" in my data, may have one or more sets of "d, f, a" from "bill['organizations']" associated with it. I would like that each set of "d, f, a" fill additional columns in the same "bill.get('measure')" row.
Upvotes: 0
Views: 360
Reputation: 2768
what about this?
with open (path + "/" + 'maplight data 113congress.json',"r") as f:
data = json.load(f)
for bill in data['bills']:
b = bill.get('measure')
tmp = [(x.get('name'), x.get('catcode'), x.get('disposition')) for x in bill['organizations']]
outputwriter.writerow(chain((b), *tmp))
outputfile.flush()
you need to:
from itertools import chain
Upvotes: 1