Reputation: 1
I have the following data taken from an API. I am trying to access the "transcript" parts using a Python script with a loop. My aim is to print all "transcript" plain text. I working on that still, I have this error:
TypeError: list indices must be integers or slices, not str.
code:
import json
filename = "1transcript.json"
with open(filename, "r") as data_file:
data = json.load(data_file)
for output in data['results']:
print(output['alternatives']['transcript']) #updated
json:
{
"@type": "type.googleapis.com/google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse",
"results": [
{
"alternatives": [
{
"confidence": 0.9172556,
"transcript": "The Joe Rogan Experience reacting to him knocking out volkov. This is one of my favorite one of my favorite Clips because that knockout was so crazy. We were in the middle here is or here. I'm so too right now, Jamie."
}
],
"languageCode": "en-us"
},
{
"alternatives": [
{
"confidence": 0.7491517,
"transcript": " Yeah, I just airdrop tattoo."
}
],
"languageCode": "en-us"
},
{
"alternatives": [
{
"confidence": 0.8975618,
"transcript": " I would not working."
}
],
"languageCode": "en-us"
},
{
"alternatives": [
{
"confidence": 0.8619629,
"transcript": " Is it going through?"
}
],
"languageCode": "en-us"
},
Upvotes: 0
Views: 119
Reputation: 1
Finally I solve it:
for output in data['results']:
print(output['alternatives'][0]['transcript'])
Upvotes: 0
Reputation: 107
for output in data['results']:
print(output['alternatives']['transcript']
print()
needs to be indented correctly. In other words: move your print
statement below output
Upvotes: 0
Reputation: 12558
You missed that alternatives
contains a list
of dict
.
print(output['alternatives'][0]['transcript']
# There is a list here ------^
Upvotes: 1