从字典中获取所有元素并将其附加到string中



下面有一个类似字典的

'results': [{'alternatives': [{'confidence': 0.77,
'transcript': 'everything that you saw it had meaning every hold three more moment of happiness '}],
'final': True},
{'alternatives': [{'confidence': 0.75,
'transcript': 'none of it matters as you like bleeding out on the battlefield '}],
'final': True},
{'alternatives': [{'confidence': 0.82,
'transcript': 'none of it changes what is speeding rock does to a body we all '}],
'final': True},
{'alternatives': [{'confidence': 0.8,
'transcript': 'what does that mean our lives are meaningless '}],
'final': True},
{'alternatives': [{'confidence': 0.9,
'transcript': 'does that mean that there was no point not being born '}],
'final': True},
{'alternatives': [{'confidence': 0.66,
'transcript': 'would you say that about slain comrades '}],
'final': True},
{'alternatives': [{'confidence': 1.0, 'transcript': 'what about '}],
'final': True},
{'alternatives': [{'confidence': 0.21,
'transcript': 'I was very meaningless '}],
'final': True},
{'alternatives': [{'confidence': 0.66,
'transcript': "they were not meeting because we don't know what "}],
'final': True}]}

但是我想将'transcript'键中的所有元素附加到字符串值。在python中怎么做呢?

我试过了

text = res['results'][0]['alternatives'][0]['transcript']
text

但是我只得到了第一个转录键的输出,在这种情况下"你看到的一切都有意义",每隔三次都有幸福的时刻。我怎样才能得到所有的抄本并把它们作为一个句子附加起来?例如,你所看到的每件事都有意义,每多保持三个幸福的时刻。这些都不重要,因为你喜欢在战场上流血.....

下面的代码将请求的字符串合并为单个字符串

data = {'results': [{'alternatives': [{'confidence': 0.77,
'transcript': 'everything that you saw it had meaning every hold three more moment of happiness '}],
'final': True},
{'alternatives': [{'confidence': 0.75,
'transcript': 'none of it matters as you like bleeding out on the battlefield '}],
'final': True},
{'alternatives': [{'confidence': 0.82,
'transcript': 'none of it changes what is speeding rock does to a body we all '}],
'final': True},
{'alternatives': [{'confidence': 0.8,
'transcript': 'what does that mean our lives are meaningless '}],
'final': True},
{'alternatives': [{'confidence': 0.9,
'transcript': 'does that mean that there was no point not being born '}],
'final': True},
{'alternatives': [{'confidence': 0.66,
'transcript': 'would you say that about slain comrades '}],
'final': True},
{'alternatives': [{'confidence': 1.0, 'transcript': 'what about '}],
'final': True},
{'alternatives': [{'confidence': 0.21,
'transcript': 'I was very meaningless '}],
'final': True},
{'alternatives': [{'confidence': 0.66,
'transcript': "they were not meeting because we don't know what "}],
'final': True}]}
combined_transcript = ' '.join(x['alternatives'][0]['transcript'] for x in data['results'])
print(combined_transcript)

输出
everything that you saw it had meaning every hold three more moment of happiness  none of it matters as you like bleeding out on the battlefield  none of it changes what is speeding rock does to a body we all  what does that mean our lives are meaningless  does that mean that there was no point not being born  would you say that about slain comrades  what about  I was very meaningless  they were not meeting because we don't know what 

相关内容

  • 没有找到相关文章

最新更新