首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从json文件中获取特定值。

从json文件中获取特定值。
EN

Stack Overflow用户
提问于 2019-02-08 15:39:14
回答 3查看 72关注 0票数 0

当我试图打印json文件时,我得到以下信息:

代码语言:javascript
复制
{'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}

我如何只打印“成绩单”部分为每一个(基本上所有的记录)?我试着运行以下代码:

代码语言:javascript
复制
for string in distros_dict.results.alternatives.items():
    print (value['transcript'])

但我说错了

代码语言:javascript
复制
AttributeError: 'dict' object has no attribute 'results'

我跑了

代码语言:javascript
复制
print(distros_dict['results'][0]['alternatives'][0]['transcript'])

它打印了正确的第一个(但我不能迭代它)

代码语言:javascript
复制
hi 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-08 15:45:21

您可以按如下方式循环字典,因为distros_dict['results']是长度为1的单个列表。

代码语言:javascript
复制
for string in distros_dict['results']:
    print(string['alternatives'][0]['transcript'])

输出

代码语言:javascript
复制
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 

...
...
票数 0
EN

Stack Overflow用户

发布于 2019-02-08 15:47:14

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

代码语言:javascript
复制
results_array = distros_dict['results']

然后,生成的数组将为您提供一个“备选方案”数组。

代码语言:javascript
复制
for result in results_array:
    alternatives_array = result['alternatives']

我认为您只需深入研究两个层次的数组。如果您只需要第一个结果,您可以简单地使用与您所拥有的类似的distros_dict['results'][0],但是如果您有多个结果,最好迭代它们。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2019-02-08 16:04:34

试试这个:

代码语言:javascript
复制
for item in distros_dict["results"]:
    for alternative in item["alternatives"]:
        print(alternative["transcript"])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54595770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档