-3

here's part of sample in caption_file

[{'text': 'okay Wow', 'start': 0.03, 'duration': 6.499}, {'text': '[Music]', 'start': 8.3, 'duration': 5.919}]

i have a bunch of samples in CAPTIONS_DIR and i want to gather some item into another dictionary

import os

from .step import Step
from yt_concate.setting import CAPTIONS_DIR

class ReadCaption(Step):
def process(self, data, inputs, utils):
    for caption_file in os.listdir(CAPTIONS_DIR):
        with open(os.path.join(CAPTIONS_DIR, caption_file), "r") as f:
            captions = {}
            for line in f:
                caption = line["text"]
                time = str(line["start"]) + "-->" + str(line["start"])
                captions[caption] = time
            print(captions)

the result and where i've been trapped

caption = line["text"]
TypeError: string indices must be integers

but if i enter a integer 0 it'll come up "["

for line in f:
    print(line[0])
deceze
  • 510,633
  • 85
  • 743
  • 889
kinslersi
  • 11
  • 3
  • You're just reading the file **as text**. If you want to turn it into an actual list of dicts, try `ast.literal_eval`. – deceze Mar 15 '22 at 09:50
  • Your file looks like it's in json format, what about trying to parse the content in json? – maya Mar 15 '22 at 09:51
  • 1
    @maya That's not JSON. – deceze Mar 15 '22 at 09:51
  • `with open(..) as f: data = ast.literal_eval(f.read())` `for line in data: print(line['text'])`… Assuming that "part of the caption file" is representative and it contains exactly one list. – deceze Mar 16 '22 at 07:40
  • @deceze Thanks for helping. I'll chewing on it – kinslersi Mar 16 '22 at 07:53

1 Answers1

-1

Have you tried this -

print(line[0]['text'])
Krishna Gupta
  • 166
  • 1
  • 10
  • yeah i have tried and the same error – kinslersi Mar 15 '22 at 09:57
  • If `line["text"]` already says `string indices must be integers`, that means **`line` is a string**. Using even more list offsets on it won't help. – deceze Mar 15 '22 at 09:59
  • @deceze i see. but after i search on the internet, i found the only way to grab index from string is to use integer in line – kinslersi Mar 15 '22 at 10:04
  • @kinslersi See my comment under your question and the duplicate linked on top of your question. This here is a dead end. – deceze Mar 15 '22 at 10:08
  • @deceze Thank you for your time! But sorry I still not get it. I tried ast.literal_eval(), json.loads(), etc and went wrong in the end. In the other hand, I was wondering the reason after I convert it and it'll eventually come up the same list of dict. – kinslersi Mar 16 '22 at 07:06
  • @deceze I'm kind of mess up now and the result underneath TypeError: string indices must be integers # for line in f: data = dict(ChainMap(*line)) – kinslersi Mar 16 '22 at 07:14