![](/img/trans.png)
[英]Python CSV writer - writing columns in new csv file up to maximum number of fields in csv files
[英]Python CSV writer: writing uneven columns
我想用不均勻的列寫行。 下面是我的代碼:
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();
我數據中的每個“ bill.get('measure')”都可能具有來自“ bill ['organizations]]”的一組或多組“ d,f,a”。 我希望每組“ d,f,a”在同一“ bill.get('measure')”行中填充其他列。
那這個呢?
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()
你需要:
from itertools import chain
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.