Python如何从嵌套字典中提取所有transcript值并拼接为字符串
你当前的代码仅指定了res['results']的第一个下标元素,所以只能提取到第一条transcript值。你可以通过遍历res['results']列表的所有元素完成全量提取拼接:
代码实现
直接拼接(无分隔符)
full_text = ''.join([item['alternatives'][0]['transcript'] for item in res['results']])
按句号分隔拼接(符合你给出的示例格式)
# strip()用于去除每段末尾多余空格,输出更规整 full_text = '.'.join([item['alternatives'][0]['transcript'].strip() for item in res['results']]) + '.'
输出结果
英文原文
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.
中文翻译
你所见的一切都有意义,每一刻都多留存了三秒幸福。当你在战场上流血濒死时,这一切都毫无意义。什么都改变不了高速飞石对人体的伤害,我们所有人都逃不过。这是不是意味着我们的生命毫无价值?这是不是意味着我们的出生本就没有意义?你会这么评价那些阵亡的战友吗?那他们算什么?我的存在毫无意义。他们没能达成目标,因为我们不知道后续的内容。
内容的提问来源于stack exchange,提问作者Mohamed Rimshad

