Python 类型错误 "list indices must be integers, not str"
Python TypeError "list indices must be integers, not str"
正在尝试将 json 转换为 csv,但为什么不起作用?
出现以下错误:
x['Emotions']['Confidence'][0], TypeError: list indices must be
integers, not str
for person in output_json["Record"]:
csv_data = person["Person"]
for x in csv_data:
f = csv.writer(open('/tmp/test.csv', 'wb+'))
f.writerow(["FrameNumber", "FrameTimePosition", "Gender", "Emotions_Type1", "Emotions_Confidence1",
"Emotions_Type2", "Emotions_Confidence2", "Emotions_Type3", "Emotions_Confidence3",
"AgeRange_High", "AgeRange_low"])
print(x['FrameNumber'])
print(x['FrameTimePosition'])
print(x['Gender']['Value'])
f.writerow([x['FrameNumber'],
x['FrameTimePosition'],
x['Gender']['Value'],
x['Emotions']['Confidence'][0],
x['Emotions']['Type'][0],
x['Emotions']['Confidence'][1],
x['Emotions']['Type'][1],
x['Emotions']['Confidence'][2],
x['Emotions']['Type'][2],
x['AgeRange']['High'],
x['AgeRange']['Low']])
下面的Json,我已经按照输出格式了,就是不知道为什么不行
{
"Record": [
{
"Person": [
{
"FrameNumber": 1,
"FrameTimePosition": "0:00:01",
"Gender": {
"Confidence": 99.86161041259766,
"Value": "Male"
},
"Emotions": [
{
"Confidence": 83.7345199584961,
"Type": "HAPPY"
},
{
"Confidence": 3.3157408237457275,
"Type": "CONFUSED"
},
{
"Confidence": 1.5936851501464844,
"Type": "CALM"
}
],
...
}
],
...
}
]
}
发生这种情况是因为您试图通过它的键获取列表值。
你可能认为它是一个字典(类似于 JSON 中的对象 {}),但它是一个列表(JSON 中的数组 [])。
我建议您检查 JSON 中的类型,特别是错误中出现的键
x['Emotions']['Confidence'][0]
您的 x['Emotions']
可能是一个数组,而不是您的基础 JSON 中的对象。
编辑JSON
x['Emotions']
是一个数组(在 python 中列出),我想你可能想要用这一行来获取键 'Confidence'
的值,所以你需要颠倒顺序,像这样:
x['Emotions'][0]['Confidence']
在这里,您使用键 'Emotions'
获取列表的第一个元素,
得到这样的命令:
{
"Confidence": 83.7345199584961,
"Type": "HAPPY"
}
并且,从这个元素,'Confidence'
属性。
希望对您有所帮助!
正在尝试将 json 转换为 csv,但为什么不起作用? 出现以下错误:
x['Emotions']['Confidence'][0], TypeError: list indices must be integers, not str
for person in output_json["Record"]:
csv_data = person["Person"]
for x in csv_data:
f = csv.writer(open('/tmp/test.csv', 'wb+'))
f.writerow(["FrameNumber", "FrameTimePosition", "Gender", "Emotions_Type1", "Emotions_Confidence1",
"Emotions_Type2", "Emotions_Confidence2", "Emotions_Type3", "Emotions_Confidence3",
"AgeRange_High", "AgeRange_low"])
print(x['FrameNumber'])
print(x['FrameTimePosition'])
print(x['Gender']['Value'])
f.writerow([x['FrameNumber'],
x['FrameTimePosition'],
x['Gender']['Value'],
x['Emotions']['Confidence'][0],
x['Emotions']['Type'][0],
x['Emotions']['Confidence'][1],
x['Emotions']['Type'][1],
x['Emotions']['Confidence'][2],
x['Emotions']['Type'][2],
x['AgeRange']['High'],
x['AgeRange']['Low']])
下面的Json,我已经按照输出格式了,就是不知道为什么不行
{
"Record": [
{
"Person": [
{
"FrameNumber": 1,
"FrameTimePosition": "0:00:01",
"Gender": {
"Confidence": 99.86161041259766,
"Value": "Male"
},
"Emotions": [
{
"Confidence": 83.7345199584961,
"Type": "HAPPY"
},
{
"Confidence": 3.3157408237457275,
"Type": "CONFUSED"
},
{
"Confidence": 1.5936851501464844,
"Type": "CALM"
}
],
...
}
],
...
}
]
}
发生这种情况是因为您试图通过它的键获取列表值。 你可能认为它是一个字典(类似于 JSON 中的对象 {}),但它是一个列表(JSON 中的数组 [])。
我建议您检查 JSON 中的类型,特别是错误中出现的键
x['Emotions']['Confidence'][0]
您的 x['Emotions']
可能是一个数组,而不是您的基础 JSON 中的对象。
编辑JSON
x['Emotions']
是一个数组(在 python 中列出),我想你可能想要用这一行来获取键 'Confidence'
的值,所以你需要颠倒顺序,像这样:
x['Emotions'][0]['Confidence']
在这里,您使用键 'Emotions'
获取列表的第一个元素,
得到这样的命令:
{
"Confidence": 83.7345199584961,
"Type": "HAPPY"
}
并且,从这个元素,'Confidence'
属性。
希望对您有所帮助!