【发布时间】:2018-06-16 06:04:41
【问题描述】:
我实际上尝试在 data.csv 文件中编写 JSON。我尝试了stackoverflow的以下解决方案: How do I write a Python dictionary to a csv file?
所以我想出了这些:
with open("data/dataGold.csv", 'w') as f:
w = csv.DictWriter(f, ['data']['user']['repositories']['nodes'], extrasaction='ignore')
w.writeheader()
w.writerow(response)
w.writerow([data['data']['user']['repositories']['nodes']['name'],
data['data']['user']['repositories']['nodes']['forkCount'],
data['data']['user']['repositories']['nodes']['issues']])
我的“dict”类型的响应变量是:
{'data': {'user': {'name': 'Markus Goldstein',
'repositories': {'nodes': [{'forkCount': 0,
'issues': {'totalCount': 0},
'name': 'repache'},
{'forkCount': 4,
'issues': {'totalCount': 3},
'name': 'nf-hishape'},
{'forkCount': 4,
'issues': {'totalCount': 7},
'name': 'ip-countryside'},
{'forkCount': 42,
'issues': {'totalCount': 29},
'name': 'bonesi'},
{'forkCount': 13,
'issues': {'totalCount': 3},
'name': 'rapidminer-anomalydetection'},
{'forkCount': 0,
'issues': {'totalCount': 0},
'name': 'rapidminer-studio'}]}}}}
有一个 TypeError 表示不允许使用索引。我认为那是因为我使用了 ['data']['user']['repositories']['nodes']。
我发布上面链接的解决方案有效,因为没有嵌套的 Dict/JSON。所以我不知道在我的情况下如何使用嵌套的 Dict/JSON
所以我的目标是包含 name、forkCount 和 issues 作为标题的 CSV。下一行是不同 repo 的值。
愿有人可以帮助我,并为我的英语不好感到抱歉-.- 谢谢!
【问题讨论】:
标签: json python-3.x csv dictionary