简体   繁体   中英

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.

I have about 30 of such csv files and would like to process all of them into a single comprehensive list. File names are iterative yet they can be defined by the number in the middle. Ptrain0dataset, Ptrain50dataset, Ptrain100dataset ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM