Reputation: 2316
I have a csv file (tab delimited) whose sample format is:
"col1 col2 col3 col4 col5"
"15 AMC 0.0 0.0 0.0"
Now I have a variable n
which denotes the number of values that I want to read from the header of this csv file. So if n = 3
so I want to read first 3 values in the header and form a list like ['col1','col2','col3']
. I wrote this code to read tab delimited csv file but I am having some issue:
n = 3 # number of values to be extracted from header of csv.Will vary
file = open('file1.csv','rU')
read = csv.reader(file,delimiter='\t')
row_number = 0
for row in read:
if row_number == 0:
header = row
break
When I do print header
I get ['col1\tcol2\tcol3\tcol4\tcol5']
. How can I extract the n
number of values from the header of the csv file?
Upvotes: 0
Views: 164
Reputation: 73490
I assume that the double quotes ("
) are in the file as you show them, so they will quote the whole line and delimiters are ignored within quoted strings. You would have to strip those quotes before handing the lines to the csv reader:
file = open('file1.csv','rU')
lines = [line.strip().strip('"') for line in file] # strip white space, too, to be sure
read = csv.reader(lines, delimiter='\t')
# any iterable producing strings will do in the reader constructor
Upvotes: 2
Reputation: 174690
What you have is not a CSV file, but simply a file with two lines that are quoted. Trying to parse this with the csv
module will just lead to errors.
Instead, do this:
n = 3
with open('file1.csv') as f
header = next(f)[1:-1].split('')
print('Header: {}'.format(header))
print('First {} columns of header: {}'.format(n, header[:n]))
for line in f:
row = line[1:-1].split('')
print('Complete row: {}'.format(row))
print('First {} columns: {}'.format(n, row[:n]))
Upvotes: 0
Reputation: 2527
My guess is that it doesn't split it is because it's not delimited by tab but by 4 spaces. To get only an amount of n you write row[:n]. Try this:
n = 3 # number of values to be extracted from header of csv.Will vary
file = open('file1.csv','rU')
read = csv.reader(file,delimiter=' '*4)
row_number = 0
for row in read:
if row_number == 0:
header = row[:n]
break
Upvotes: -1