【发布时间】:2015-12-09 20:21:36
【问题描述】:
我有两个列表,我想将它们合并到一个数组中,最后将它放入一个 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)
得到了:
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
我的计数如何工作?
【问题讨论】:
-
您明确地将
array_length设为字符串,因此i是字符而不是数字... -
哎呀,我看的不对……谢谢!!
-
当我以为我正在解析
{thisthing}而实际上我正在解析[{thisthing}]时,这发生在我身上;例如,我正在尝试处理字典,但实际上我正在处理列表。动态打字ftw。