I have a dictionary-like below
'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}]}
but I want to append all the elements in the 'transcript' key to a string value. How can I do that in python?
I have tried
text = res['results'][0]['alternatives'][0]['transcript']
text
But I am only getting the output in the first transcript key, in this case "everything that you saw it had meaning every hold three more moment of happiness". How can I get all of the transcript and append them as a sentence. For example, 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.....
CodePudding user response:
The code below combines the requested string into a single string
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)
output
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