Python CSV writer: writing uneven columns -
i want write rows uneven columns. below code:
import csv import json path = 'e:/thesis/thesis_get_data' outputfile = open('maplight_113.csv', 'w', newline='') outputwriter = csv.writer(outputfile) open (path + "/" + 'maplight data 113congress.json',"r") f: data = json.load(f) bill in data['bills']: b = bill.get('measure') organization in bill['organizations']: d = organization.get('name') f = organization.get('disposition') = (organization.get('catcode')) outputwriter.writerow([b, d, a, f]) outputfile.flush(); every "bill.get('measure')" in data, may have 1 or more sets of "d, f, a" "bill['organizations']" associated it. each set of "d, f, a" fill additional columns in same "bill.get('measure')" row.
what this?
with open (path + "/" + 'maplight data 113congress.json',"r") f: data = json.load(f) bill in data['bills']: b = bill.get('measure') tmp = [(x.get('name'), x.get('catcode'), x.get('disposition')) x in bill['organizations']] outputwriter.writerow(chain((b), *tmp)) outputfile.flush() you need to:
from itertools import chain
Comments
Post a Comment