我有一个类似于dt1的json响应,我将json数据写入CSV,其中包含字段名、Total和details,下面是我的代码。在dt1 = { u'Name': ABC,
u'total': 6 ,
u'Details':{
u'Subject1': {u'Opted': False, u'value': u'100'},
u'Subject2': {u'Opted': True, u'value': u'200'},
u'Subject2': {u'Opted': True, u'value': u'200'}
}
}
with open('file.csv', 'w') as csvfile:
fieldnames = ['Name', 'Total', 'Details']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
dt2 = dt1.values()[0].keys(
for key in dt1.keys():
if dt1[key]['detected'] is True)
writer.writerow({'NAME': dt1['Name'], 'Total' : dt1['Total'], 'Details': ([key, [dt1[key][scan] for scan in dt2]])})
CSV生成的是:
^{pr2}$
我想把details dict的多行数据写入一列,如下所示。名字和总数不重复每个主题。有可能吗?我如何实现它。在NAME Total Details
ABC 6 Subject1, "[u'Opted': False, u'value': u'100']"
Subject2, "[u'Opted': True, u'value': u'200']"
Subject3, {u'Opted': True, u'value': u'500'}