0

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"
    },
3
  • You miss the closing ) of your print Commented Mar 20, 2021 at 22:17
  • Thank you. But still, code doesn't work :( @martineau TypeError: list indices must be integers or slices, not str Commented Mar 20, 2021 at 22:19
  • In that case update your question. Commented Mar 20, 2021 at 22:20

3 Answers 3

1

You missed that alternatives contains a list of dict.

print(output['alternatives'][0]['transcript']
# There is a list here ------^
Sign up to request clarification or add additional context in comments.

2 Comments

still something missing IndentationError: expected an indented block
@PiotrSzymański Indent your print statement with 4 spaces.
0
for output in data['results']:
    print(output['alternatives']['transcript']

print() needs to be indented correctly. In other words: move your print statement below output

Comments

0

Finally I solve it:

for output in data['results']:
    print(output['alternatives'][0]['transcript'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.