相关疑难解决方法(0)

TypeError:list indices必须是整数或切片,而不是str

我有两个列表,我想在一个数组中合并,最后把它放在一个csv文件中.我是Python的数组的新手,我不明白我怎么能避免这个错误:

def fill_csv(self, array_urls, array_dates, csv_file_path):
    result_array = []
    array_length = str(len(array_dates))

    # We fill the CSV file
    file = open(csv_file_path, "w")
    csv_file = csv.writer(file, delimiter=';', lineterminator='\n')

    # We merge the two arrays in one

    for i in array_length:
        result_array[i][0].append(array_urls[i])
        result_array[i][1].append(array_dates[i])
        i += 1

    csv_file.writerows(result_array)
Run Code Online (Sandbox Code Playgroud)

得到了:

  File "C:\Users\--\gcscan.py", line 63, in fill_csv
    result_array[i][0].append(array_urls[i])
TypeError: list indices must be integers or slices, not str
Run Code Online (Sandbox Code Playgroud)

我的计数如何运作?

python csv arrays list

37
推荐指数
3
解决办法
35万
查看次数

TypeError:list indices必须是整数,而不是str(实际是boolean convertion)

import nltk
import random
from nltk.corpus import movie_reviews

documents=[(list(movie_reviews.words(fileid)),category)
           for category in movie_reviews.categories()
           for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)
#print(documents[1])

all_words=[]

for w in movie_reviews.words():
    all_words.append(w.lower())

all_words=nltk.FreqDist(all_words)

word_features = list(all_words.keys())[:3000]

def find_features(document):
    words = set(document)
    features=[]
    for w in word_features:
        features[w]= (w in words)

    return features

print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))

featuresets = [(find_features(rev), category) for (rev,category) in documents]
Run Code Online (Sandbox Code Playgroud)

运行后,我收到错误

features[w]= (w in words)
TypeError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

请帮我解决一下......

python find review movie

5
推荐指数
2
解决办法
4万
查看次数

标签 统计

python ×2

arrays ×1

csv ×1

find ×1

list ×1

movie ×1

review ×1