Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DictReader, No quotes, tabbed file

Tags:

I have a csv file that looks like this: Please note, there are no quotes, a tab (\t) is the delimiter, and there is a blank line between the header and the actual content.

Facility No     Testing No      Name    Age  252     2351    Jackrabbit, Jazz        15 345     257     Aardvark, Ethel 41 

I think I've tried nearly every possible combination of ideas and parameters

f = open('/tmp/test', 'r') csvFile = f.read() reader = csv.DictReader(csvFile, delimiter='\t', quoting=csv.QUOTE_NONE) print reader.fieldnames 

the result of the print is:

['F'] 

How can I get this into something I can parse to put into a database? Getting it into a dictionary would be helpful.

like image 740
Mike Avatar asked Mar 19 '11 08:03

Mike


2 Answers

What is your csvFile? Is it a string representing your filename starting with 'F'?

csv.DictReader needs an opened file object, not a filename.

Try:

with open(csvFile, 'rb') as f:     reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)     print reader.fieldnames 

EDIT

If your csvFile is a string containing the whole data, you will have to convert it into a StringIO (because csv can access only file-like objects, not strings).

Try:

from cStringIO import StringIO  # csvFile = 'Facility No\tTesting No\tName\tAge\n\n252\t2351\tJackrabbit, Jazz\t15\n345\t257\tAardvark, Ethel\t41\n' reader = csv.DictReader(StringIO(csvFile), delimiter='\t', quoting=csv.QUOTE_NONE) print reader.fieldnames 

Or, if your edited question opens and reads a file:

with open('/tmp/test', 'rb') as f:     reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)     print reader.fieldnames 

This works for me.

like image 152
eumiro Avatar answered Nov 26 '22 23:11

eumiro


this might work for you, at least as a start:

 >>> import csv >>> input = open('/tmp/csvtemp.csv') >>> csvin = csv.reader(input, delimiter='\t') >>> data = [row for row in csvin] >>> header = data.pop(0) >>> data.pop(0)  # skip blank line [] >>> for row in data: ...  rowdict = dict(zip(header, row)) ...  print rowdict ...  {'Age': '15', 'Testing No': '2351', 'Name': 'Jackrabbit, Jazz', 'Facility No': '252'} {'Age': '41', 'Testing No': '257', 'Name': 'Aardvark, Ethel', 'Facility No': '345'} 
like image 42
jcomeau_ictx Avatar answered Nov 26 '22 23:11

jcomeau_ictx