Jason Donnald Jason Donnald - 6 months ago 14
Python Question

issue in reading n number of values from a csv file header in python

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?

Answer

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