2

I have a variable with row index numbers that I want to grab from one CSV file and write those rows into a new one.

CSV file that I am trying to copy from looks like this:

sdkgsk;74785797;hdfgsdgfjss

asdjkhfgsdkjg;9857349834;jsdhfg

skhgf;76578346534;jhfgsdjhgdf

sdifl;56975654;kjdjh

afhkddf;439587349346;hsfd

kdgfdkj;983496;hioas

oish;89468468;jhfdsgsdf

jksdfjhksdfjk;8968;jkfdsjhksd

I had tried multiple things but this is the last one:

row_index = [2,4,6]

with open('test.csv', 'r') as f,open('out.csv', 'w') as f_out:
    reader = csv.reader(f)
    writer = csv.writer(f_out)
    lines = list(reader)
    for row in lines:
        writer.writerow(row[row_index])

I was expecting to generate a new file under the name 'out.csv' that would contain rows 2,4 and 6 from 'test.csv'

Instead I am getting this error message :"list indices must be integers or slices, not list"

1 Answer 1

3

The problem is with this line:

writer.writerow(row[row_index])

row_index is a list and you can't index a list of integers(row) with another list. This is exactly what python interpreter try to tell you with this error messsage: list indices must be integers or slices, not list

What you can do instead is using list comprehension to create new_row based on row_index list:

for row in lines:
    new_row = [row[index] for index in row_index]
    writer.writerow(new_row)

EDIT: There is another issue I missed.Based on your code I thought you want to rewrite column 2,4 and 6 from each row, but what you really want to index are rows, not colums. So you want to rewrite full rows 2,4 and 6. In that case you can again use list comprehension.

with open('test.csv', 'r') as f,open('out.csv', 'w') as f_out:
    reader = csv.reader(f)
    writer = csv.writer(f_out)
    lines = list(reader)
    new_rows = [lines[index] for index in row_index]
    writer.writerows(new_rows)

And again list index out of range error message tells you exactly what is wrong. Please try reading and interpreting error messages carefully next time.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply... The problem that I am getting now is "list index out of range" for row "new_row = [row[index] for index in row_index]"
If I use your EDITED code I get the following error :" list indices must be integers or slices, not NoneType" ..... that's also for the line "new_rows = [lines[index] for index in row_index]"
YES, because I did not write full program for you, just added important part so it does not work as is. I guess you're smart enough to find what's missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.