gpt4 book ai didi

python - JSON(列表,不是字典)到 csv 文件 - PYTHON

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:47 25 4
gpt4 key购买 nike

我正在尝试使用 python 将 JSON 文件转换为 csv。

我尝试了将数据放入字典的方法,但是我的 JSON 文件位于列表中,因此它永远不起作用。它不采用字符串索引。

import json
from collections import OrderedDict

with open('RA_1.json', 'r') as f:
data_dict = json.load(f)

for data in data_dict:
print(data['result']['specific_issues']['bills_by_algo'] ['sponsor'])

“这是我的 JSON 文件:”

"result": [
{
"issue_codes": [
"BUD",
"EDU"
],
"received": 1436367560,
"has_amendments": false,
"client_name": "Apple Inc",
"period": "2nd Quarter (Apr 1 - June 30)",
"registrant": "Bernstein Strategy Group",
"amount": 20000,
"client": {
"legal_name": "Apple Inc",
"name": "Apple, Inc.",
"naics": "334111",
"gvkey": "1690",
"ticker": "AAPL",
"id": null,
"bvdid": "US942404110"
},
"specific_issues": [
{
"text": "HR 5 -- Student Success Act -- blended learning provision S. 1177 Every Child Achieves Act -- specifically seeking addition of educational technology program and amendments related to student privacy and homework gap Unintroduced House legislation to update the Federal Education Records Privacy Act HR 2092 -- Student Digital Privacy and Parental Rights Act of 2015 S. 1341 -- Student Privacy Protection Act S. 1322 -- Protecting Student Privacy Act Unintroduced House and Senate legislation to address the homework gap Unintroduced Senate student privacy legislation \n",
"bills_by_algo": [

“还有更多”

“这是错误:”

--------
TypeError Traceback (most recent call last)
<ipython-input-8-c3b4246762f8> in <module>
6
7 for data in data_dict:
----> 8 print(data['result']['specific_issues']['bills_by_algo']['sponsor'])

TypeError: string indices must be integers

最佳答案

data_dict 中的数据 不适合您使用。当您从字典进行迭代时,您会获取其键,但不使用其 items() 方法。在这里,您在 data only 中获得了字符串“result”(我想是因为您没有给出文件结尾)。 p>

因此,删除 for 指令并只写入:

print(data_dict['result']['specific_issues']['bills_by_algo']['sponsor'])

关于python - JSON(列表,不是字典)到 csv 文件 - PYTHON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com