我有一个名为输出的数据框,看起来像 -
output
Out[48]:
({'results': [{'alternatives': [{'confidence': 0.82,
'transcript': 'thank you for calling a AA health insurance my name is Dick right the pleasure of speaking with '}],
'final': True},
{'alternatives': [{'confidence': 0.57, 'transcript': 'hi Nick this is '}],
'final': True},
{'alternatives': [{'confidence': 0.78,
'transcript': 'hi Julie I think we talked earlier we did '}],
'final': True},
{'alternatives': [{'confidence': 0.86,
'transcript': "thing else comes up or you have any questions just don't hesitate to call us okay okay thank you so much yeah you're very welcome you have a great rest your day okay you too bye bye "}],
'final': True}],
'result_index': 0},)
我试图仅访问“成绩单”并将其转换为 csv 数据框。我试过了-
output.to_csv("script.csv")
Traceback (most recent call last):
File "<ipython-input-44-85a7c839323b>", line 1, in <module>
output.to_csv("script.csv")
AttributeError: 'tuple' object has no attribute 'to_csv'
我也尝试只访问成绩单,但我得到了以下相同的错误-
print(output['results'][0]['alternatives'][0]['transcript'])
Traceback (most recent call last):
File "<ipython-input-49-03a8e1a518ee>", line 1, in <module>
print(output['results'][0]['alternatives'][0]['transcript'])
TypeError: tuple indices must be integers or slices, not str
如何避免此错误?
最佳答案
您可以从字典列表构建 pandas DataFrame。假设您只需要最里面的元素,您可以使用推导式来获取它:
df = pd.DataFrame([i for elts in output for alts in elts['results'] for i in alts['alternatives']])
您将获得以下 DataFrame:
confidence transcript
0 0.82 thank you for calling a AA health insurance my...
1 0.57 hi Nick this is
2 0.78 hi Julie I think we talked earlier we did
3 0.86 thing else comes up or you have any questions ...
关于python - 由于元组错误,无法访问多个嵌套字典中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60436354/