I am a beginner in Python. My data table (Table1) has 21 columns and thousands of rows. It is so huge I cannot open it on my laptop. It looks somewhat like this:
ABCDEFG, HIJKLMNO, PQRSTUVW, TEYHDSJD ……..
TRGFHFJ, GDGSANTO, JDKNVWWR, URNWHJX ……..
…….
However, I need to reproduce a subset of this table that only consists of columns 10-21. I have tried the following:
import sys
import csv
with open(sys.argv[1], "r") as input:
readinput = csv.reader(input, delimiter=',')
with open("output.csv", 'wt') as output:
writer=csv.writer(output, delimiter="\t")
for row in input:
values = [row[10],row[11],row[12],row[13],row[14],row[15],row[16],row[17],row[18],row[19],row[20],row[21]]
writer.writerow([values])
print (row[10])
But it turns out the cvs.reader’s expression “row[10]” is not interpreting this as the 10th element of the row (=the 10th column). Instead, it gives me the 10th letter. So the output is not the 10th to 21st columns of Table1, but it is the 10th to 21st letters, somewhat like this:
K, L, M, N, O, “”, P, Q, R, S, T, U
S, A, N, T, O, “”, J, D, K, N, V, W
What am I doing wrong? Thanks for any help!
You’re using
for row in input, I guess you wantedfor row in readinput.