从json文件中获取特定的值

时间:2019-02-08 15:39:15

标签: python json loops dictionary

当我尝试打印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 

3 个答案:

答案 0 :(得分:0)

由于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 

...
...

答案 1 :(得分:0)

您的distros_dict中有几个数组。第一个是“结果”数组。您可以通过

进行访问
results_array = distros_dict['results']

然后,结果数组为您提供一个“替代”数组

for result in results_array:
    alternatives_array = result['alternatives']

我认为您只需要深入研究2个级别的阵列。如果只需要第一个结果,则可以简单地使用distros_dict['results'][0],类似于所使用的结果,但是如果您有多个结果,最好遍历它们。

希望有帮助。

答案 2 :(得分:0)

尝试一下:

for item in distros_dict["results"]:
    for alternative in item["alternatives"]:
        print(alternative["transcript"])