[英]list indices must be integers or slices not tuple
I'm trying to reorganize my data to a specific format, and found stuck at the error message. 我试图将我的数据重组为特定格式,但发现错误消息卡住了。
I'm looking into a 16-columned csv file and I want to merge column 1-5 into a single column while also appending 10th column right next to it, while discarding the rest of the columns. 我正在寻找一个16列的csv文件,我想将列1-5合并为一个列,同时还要在其旁边追加第10列,同时丢弃其余的列。
I have about 30 of such csv files and would like to process all of them into a single comprehensive list. 我有大约30个这样的csv文件,并希望将它们全部处理到一个综合列表中。 File names are iterative yet they can be defined by the number in the middle. 文件名是迭代的,但可以由中间的数字定义。 Ptrain0dataset, Ptrain50dataset, Ptrain100dataset ... Ptrain0数据集,Ptrain50数据集,Ptrain100数据集...
Here's the code I'm juggling with: 这是我正在处理的代码:
clist=[]
for i in range(0,17):
a=i*50
with open('Ptrain' + str(a) + 'dataset,csv', 'r') as f:
temp=f.readlines()
temp2=[]
for i in temp:
j=i.replace('""', '')
temp2.append(j.split(','))
for j in temp2:
clist[str(a), j[0:5], j[9]]
f.close()
stacktrace: 堆栈跟踪:
File "<ipython-input-23-e7da8e2724f8>", line 1, in <module>
runfile('pfiles')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "pfiles/aa.py", line 35, in <module>
clist[str(a), j[0:5], j[9]]
I can't seem to figure out whats the mistake in here. 我似乎无法弄清楚这里的错误是什么。
The mistake is in this line: 错误在这一行:
clist[str(a), j[0:5], j[9]]
You are indexing a list
by a tuple, not an integer. 您正在按元组而不是整数索引list
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.