Python CSV writer 写入不平衡列

2024-09-29 23:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用不均匀的列来写行。以下是我的代码:

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();

每”比尔。得到在我的数据中,('measure')”可能有一组或多组“d,f,a”,from“bill['organizations']”与之关联。我希望每一组“d,f,a”都要在同一列中填充其他列比尔。得到('measure')”行。在


Tags: csvpathinimportjsonfordataget
1条回答
网友
1楼 · 发布于 2024-09-29 23:16:44

这个怎么样?在

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()

您需要:

^{pr2}$

相关问题 更多 >

    热门问题