I've got two list that I want to merge in one array fo finally put it in a csv file. I'm a newbie with Python's arrays and I don't understand how I can avoid this error :
我有两个列表,我想在一个数组中合并,最后把它放在一个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)
And got :
得到了:
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
How can my count work ?
我的计数如何运作?
1 个解决方案
#1
15
First, array_length
should be an integer and not a string:
首先,array_length应该是一个整数而不是一个字符串:
array_length = len(array_dates)
Second, your for
loop should be constructed using range
:
其次,你的for循环应该使用范围构造:
for i in range(array_length): # Use `xrange` for python 2.
Third, i
will increment automatically, so delete the following line:
第三,我会自动增加,所以删除以下行:
i += 1
#1
15
First, array_length
should be an integer and not a string:
首先,array_length应该是一个整数而不是一个字符串:
array_length = len(array_dates)
Second, your for
loop should be constructed using range
:
其次,你的for循环应该使用范围构造:
for i in range(array_length): # Use `xrange` for python 2.
Third, i
will increment automatically, so delete the following line:
第三,我会自动增加,所以删除以下行:
i += 1