current position:Home>TypeError: string indices must be integers

TypeError: string indices must be integers

2022-02-02 18:11:01 CSDN Q & A

def load_data(self):        reviews = pd.read_csv('tuniu.csv')          data = reviews['content']        #with open('r.json') as file:           #data = file.read()        #data = json.loads(data)        #print(type(data))        return data    def fenci_data(self):            text = []            data = self.load_data()            with open("stoplist.txt", 'r',encoding='UTF-8') as file:                stop_word_list = file.read()            for weibo_item in tqdm(data):                tmp = []                sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['content']))                for word in jieba.lcut(sentence):                    if word not in stop_word_list:                        tmp.append(word)                text.append(tmp)            return text

Report errors :
sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['content']))
TypeError: string indices must be integers




Refer to the answer 1:

That's just passing content This column is just .

sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['content']))#  Change it to sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item))



Refer to the answer 2:



Refer to the answer 3:

See Tips , This weibo_item The current value of yes character string , Not a dictionary .
Try a
sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['content']))
Change it to

try:    sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['content']))except Exception as e :    print("error",repr(e),'\n',weibo_item)    sentence = ""



Refer to the answer 4:



Refer to the answer 5:

Look at the hint
weibo_item Is string , How weibo_item['content'] ?




Refer to the answer 6:



Refer to the answer 7:

First paste the screenshots of both files , Describe your program logic .




Refer to the answer 8:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021810596721.html

Random recommended