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"