TypeError: string indices must be integers, while trying to convert json into csv
TypeError: string indices must be integers, while trying to convert json into csv
我在运行下面的代码
时收到这个错误
import json
import csv
with open ("sample-json-file.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
for item in data:
csv_file.writerow([item['name'], item['mobile'], item['boolean'], item['country']])
我的json文件有以下内容-
{
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country" : "India"
}
请提出修改意见,不胜感激
回溯
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-9719807e095c> in <module>
8 csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
9 for item in data:
---> 10 csv_file.writerow([item['name'], item['mobile'], item['boolean'], item['country']])
TypeError: string indices must be integers
如果数据是文件中的单个dict
- 给定本例中的数据
- 使用您提供的代码
for item in data:
不是必需的,因为您已经在从数据中写入密钥并且没有任何可迭代的内容。
with open ("test.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
csv_file.writerow([data['Name'], data['Mobile'], data['Boolean'], data['country']])
如果数据是文件中dicts
的list
条,如下:
[ {
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country": "India"
}, {
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country": "India"
}]
- 现在
for item in data:
需要遍历列表中的每个 dict
with open ("test.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
for item in data:
csv_file.writerow([item['Name'], item['Mobile'], item['Boolean'], item['country']])
您的示例 json 是单个 dict
。当您尝试迭代它时,您会得到字典键(在您的例子中是字符串),当然 "name"["name"]
不是问题。您可以删除 for
循环(它只是 1 个字典,而不是字典列表)和 writerow(data['nmae'], ..)
但是 csv
有一个 DictWriter
class 可以改用.
import json
import csv
with open ("sample-json-file.json") as file:
data = json.load(file)
fname = "output.csv"
fieldnames = ["Name", "Mobile", "Boolean", "Country"]
with open(fname,"w") as file:
csv_file =csv.DictWriter(file, fieldnames=fieldnames, extrasaction='ignore')
csv_file.writeheader()
csv_file.writerow(data)
我在运行下面的代码
时收到这个错误import json
import csv
with open ("sample-json-file.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
for item in data:
csv_file.writerow([item['name'], item['mobile'], item['boolean'], item['country']])
我的json文件有以下内容-
{
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country" : "India"
}
请提出修改意见,不胜感激
回溯
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-9719807e095c> in <module>
8 csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
9 for item in data:
---> 10 csv_file.writerow([item['name'], item['mobile'], item['boolean'], item['country']])
TypeError: string indices must be integers
如果数据是文件中的单个dict
- 给定本例中的数据
- 使用您提供的代码
for item in data:
不是必需的,因为您已经在从数据中写入密钥并且没有任何可迭代的内容。
with open ("test.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
csv_file.writerow([data['Name'], data['Mobile'], data['Boolean'], data['country']])
如果数据是文件中dicts
的list
条,如下:
[ {
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country": "India"
}, {
"Name": "Test",
"Mobile": 12345678,
"Boolean": "true",
"Pets": ["Dog", "cat"],
"country": "India"
}]
- 现在
for item in data:
需要遍历列表中的每个dict
with open ("test.json") as file:
data = json.load(file)
fname = "output.csv"
with open(fname,"w") as file:
csv_file =csv.writer(file)
csv_file.writerow(["Name", "Mobile", "Boolean", "Country"])
for item in data:
csv_file.writerow([item['Name'], item['Mobile'], item['Boolean'], item['country']])
您的示例 json 是单个 dict
。当您尝试迭代它时,您会得到字典键(在您的例子中是字符串),当然 "name"["name"]
不是问题。您可以删除 for
循环(它只是 1 个字典,而不是字典列表)和 writerow(data['nmae'], ..)
但是 csv
有一个 DictWriter
class 可以改用.
import json
import csv
with open ("sample-json-file.json") as file:
data = json.load(file)
fname = "output.csv"
fieldnames = ["Name", "Mobile", "Boolean", "Country"]
with open(fname,"w") as file:
csv_file =csv.DictWriter(file, fieldnames=fieldnames, extrasaction='ignore')
csv_file.writeheader()
csv_file.writerow(data)