Zol*_*oom 37 python csv arrays list
我有两个列表,我想在一个数组中合并,最后把它放在一个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)
我的计数如何运作?
Ale*_*der 30
首先,array_length
应该是一个整数而不是一个字符串:
array_length = len(array_dates)
Run Code Online (Sandbox Code Playgroud)
其次,你的for
循环应该使用range
:
for i in range(array_length): # Use `xrange` for python 2.
Run Code Online (Sandbox Code Playgroud)
第三,i
将自动增加,因此删除以下行:
i += 1
Run Code Online (Sandbox Code Playgroud)
我有同样的错误,错误是我将列表和字典添加到同一个列表(对象)中,当我曾经遍历字典列表并用来命中列表(类型)对象时,我曾经得到这个错误.
这是一个代码错误,并确保我只将字典对象添加到该列表并将键入的对象添加到列表中,这也解决了我的问题。
小智 5
跟进上述Abdeali Chandanwala 的回答(无法评论,因为代表 <50)-
TL;DR:我试图通过集中迭代字典中的键来错误地迭代字典列表,但不得不迭代字典本身!
我在使用这样的结构时遇到了同样的错误:
{
"Data":[
{
"RoomCode":"10",
"Name":"Rohit",
"Email":"rohit@123.com"
},
{
"RoomCode":"20"
"Name":"Karan",
"Email":"karan@123.com"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我试图在这样的列表中附加名称-
修复它 -
归档时间: |
|
查看次数: |
346975 次 |
最近记录: |