Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MessageToDict: How to extract keys in multiple dictionaries?

from google.protobuf.json_format import MessageToDict

audio={'results': [{'alternatives': 
                      [{'transcript': 'His name is Charlie', 'confidence': 0.9259988}], 'resultEndTime': '7.700s', 'languageCode': 'en'}, 
                   {'alternatives': 
                      [{'transcript': 'and he lives in xxx street', 'confidence': 0.9259988}], 'resultEndTime': '11.900s', 'languageCode': 'en'}
                  ], 
                   'totalBilledTime': '14s', 
                   'requestId': '68687945678899765555'}

Hi! I want to extract ‘transcript and’ ‘resultEndTime’ from my dictionary. When I did

audio['results'][0]['alternatives'][0]['transcript']

it only prints "His name is Charlie", but what I want is "His name is Charlie and he lives in xxx street". Sometimes there are more than 2 dictionaries so I do not know how to merge them.

I also only want to extract the last ‘resultEndTime’ which is ‘11.900s’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Get each transcript and join it with a space between each one, and then get the last result’s resultEndTime.

transcript = " ".join(res['alternatives'][0]['transcript'] for res in audio['results'])
end_time = audio['results'][-1]['resultEndTime']

output from print:

His name is Charlie and he lives in xxx street
11.900s
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading