当我试图打印json文件时,我得到以下信息:
{'results': [{'alternatives': [{'confidence': 0.996, 'transcript': 'hi '}], 'final': True}, {'alternatives': [{'confidence': 0.973, 'transcript': "it's my first day of school today "}], 'final': True}, {'alternatives': [{'confidence': 0.956, 'transcript': "I'm feeling nervous "}], 'final': True}, {'alternatives': [{'confidence': 0.898, 'transcript': "what if I don't know anything "}], 'final': True}, {'alternatives': [{'confidence': 0.957, 'transcript': "don't worry just give school a try dad says to take something from home to make me feel less nervous "}], 'final': True}, {'alternatives': [{'confidence': 0.948, 'transcript': "take this hard for your first day because I love you robot I know you'll do great today "}], 'final': True}, {'alternatives': [{'confidence': 0.989, 'transcript': 'thanks dad I love you too '}], 'final': True}, {'alternatives': [{'confidence': 0.81, 'transcript': 'this heart will make me think of you all day '}], 'final': True}, {'alternatives': [{'confidence': 0.911, 'transcript': 'my first class is math '}], 'final': True}, {'alternatives': [{'confidence': 0.909, 'transcript': 'will you try it with me '}], 'final': True}, {'alternatives': [{'confidence': 0.24, 'transcript': 'shapes '}], 'final': True}, {'alternatives': [{'confidence': 0.982, 'transcript': "I don't know much about shapes "}], 'final': True}, {'alternatives': [{'confidence': 0.892, 'transcript': "I'll put them in my computer "}], 'final': True}, {'alternatives': [{'confidence': 0.778, 'transcript': 'I learned three shapes '}], 'final': True}, {'alternatives': [{'confidence': 0.215, 'transcript': 'circle '}], 'final': True}, {'alternatives': [{'confidence': 0.733, 'transcript': 'tri angle '}], 'final': True}, {'alternatives': [{'confidence': 0.918, 'transcript': 'where '}], 'final': True}, {'alternatives': [{'confidence': 0.5, 'transcript': 'well I liked doing math '}], 'final': True}, {'alternatives': [{'confidence': 0.915, 'transcript': "now it's time for art class "}], 'final': True}, {'alternatives': [{'confidence': 0.867, 'transcript': "I haven't made a lot of art before "}], 'final': True}, {'alternatives': [{'confidence': 0.912, 'transcript': 'but I can try right '}], 'final': True}, {'alternatives': [{'confidence': 0.909, 'transcript': "I've never painted before "}], 'final': True}, {'alternatives': [{'confidence': 0.864, 'transcript': 'have you '}], 'final': True}, {'alternatives': [{'confidence': 0.998, 'transcript': "well I'll give it a try "}], 'final': True}, {'alternatives': [{'confidence': 0.603, 'transcript': 'there '}], 'final': True}, {'alternatives': [{'confidence': 0.995, 'transcript': 'what do you think '}], 'final': True}, {'alternatives': [{'confidence': 0.967, 'transcript': 'I am more artistic than I thought '}], 'final': True}, {'alternatives': [{'confidence': 0.752, 'transcript': 'I like art class '}], 'final': True}, {'alternatives': [{'confidence': 0.993, 'transcript': "now it's time for recess "}], 'final': True}, {'alternatives': [{'confidence': 0.773, 'transcript': "that's when we get to play outside "}], 'final': True}, {'alternatives': [{'confidence': 0.978, 'transcript': 'do you want to play with me '}], 'final': True}, {'alternatives': [{'confidence': 0.802, 'transcript': 'Hey look I jungle gym '}], 'final': True}, {'alternatives': [{'confidence': 0.773, 'transcript': "I've never been on a jungle gym before "}], 'final': True}, {'alternatives': [{'confidence': 0.999, 'transcript': 'is this how this is supposed to work '}], 'final': True}, {'alternatives': [{'confidence': 0.646, 'transcript': 'I loved recess '}], 'final': True}, {'alternatives': [{'confidence': 0.943, 'transcript': 'and I love school '}], 'final': True}, {'alternatives': [{'confidence': 0.822, 'transcript': 'I can do so much and I learned some things to '}], 'final': True}, {'alternatives': [{'confidence': 0.707, 'transcript': 'look here comes '}], 'final': True}, {'alternatives': [{'confidence': 0.97, 'transcript': "I'm so proud of you on your first day I have a present for you "}], 'final': True}, {'alternatives': [{'confidence': 0.783, 'transcript': 'I '}], 'final': True}, {'alternatives': [{'confidence': 0.445, 'transcript': 'love '}], 'final': True}, {'alternatives': [{'confidence': 0.346, 'transcript': 'cool '}], 'final': True}, {'alternatives': [{'confidence': 0.451, 'transcript': "it's true "}], 'final': True}, {'alternatives': [{'confidence': 0.838, 'transcript': 'how do I look '}], 'final': True}, {'alternatives': [{'confidence': 0.836, 'transcript': 'like a robot who loves school '}], 'final': True}], 'result_index': 0}我如何只打印“成绩单”部分为每一个(基本上所有的记录)?我试着运行以下代码:
for string in distros_dict.results.alternatives.items():
print (value['transcript'])但我说错了
AttributeError: 'dict' object has no attribute 'results'我跑了
print(distros_dict['results'][0]['alternatives'][0]['transcript'])它打印了正确的第一个(但我不能迭代它)
hi 发布于 2019-02-08 15:45:21
您可以按如下方式循环字典,因为distros_dict['results']是长度为1的单个列表。
for string in distros_dict['results']:
print(string['alternatives'][0]['transcript'])输出
hi
it's my first day of school today
I'm feeling nervous
what if I don't know anything
don't worry just give school a try dad says to take something from home to make me feel less nervous
take this hard for your first day because I love you robot I know you'll do great today
thanks dad I love you too
this heart will make me think of you all day
...
...发布于 2019-02-08 15:47:14
您的distros_dict中有两个数组。第一个是“结果”数组。你可以通过
results_array = distros_dict['results']然后,生成的数组将为您提供一个“备选方案”数组。
for result in results_array:
alternatives_array = result['alternatives']我认为您只需深入研究两个层次的数组。如果您只需要第一个结果,您可以简单地使用与您所拥有的类似的distros_dict['results'][0],但是如果您有多个结果,最好迭代它们。
希望这能有所帮助。
发布于 2019-02-08 16:04:34
试试这个:
for item in distros_dict["results"]:
for alternative in item["alternatives"]:
print(alternative["transcript"])https://stackoverflow.com/questions/54595770
复制相似问题