471,330 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Multiple delimiters with csv DictReader

2
I have a csv file that is mainly comma seperated, but one of the fields has currency values in the form "$1,378.59". By just specifying a delimiter:',' in my dialect, the above value gets parsed as 1.00 and 378.59. I would also like to include ' " ' as an additional delimiter so that the string inside the quotes gets read as one string. Is there a way I can do this or a workaround? Thanks!!
Sep 26 '07 #1
3 12339
bvdet
2,851 Expert Mod 2GB
I have a csv file that is mainly comma seperated, but one of the fields has currency values in the form "$1,378.59". By just specifying a delimiter:',' in my dialect, the above value gets parsed as 1.00 and 378.59. I would also like to include ' " ' as an additional delimiter so that the string inside the quotes gets read as one string. Is there a way I can do this or a workaround? Thanks!!
Using the csv module:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2.  
  3. f = open('csv.txt', 'rb')
  4. for item in csv.reader(f):
  5.     print item
Sample output:
['Blues', 'Elwood', '1060 W Addison', 'Chicago, Illinois 60613']

Notice the comma in the last item. Any time a field has a comma, it must be enclosed by quotes like this:

Blues,Elwood,"1060 W Addison","Chicago, Illinois 60613"
Sep 26 '07 #2
nuatpy
2
Thanks for your response. So you are saying that the csv reader should automatically take care of the thousand-seperator if the value is enclosed in quotes. Heres what I am using:

class dialect(csv.excel):
delimiter = '\t'

py_dict = csv.DictReader(fileHandle,dialect=dialect)

And it somehow fails to recognize the quotes as a delimiter. Is it because I am overriding it with '\t'?
Sep 26 '07 #3
bvdet
2,851 Expert Mod 2GB
Thanks for your response. So you are saying that the csv reader should automatically take care of the thousand-seperator if the value is enclosed in quotes. Heres what I am using:

class dialect(csv.excel):
delimiter = '\t'

py_dict = csv.DictReader(fileHandle,dialect=dialect)

And it somehow fails to recognize the quotes as a delimiter. Is it because I am overriding it with '\t'?
Try this:
Expand|Select|Wrap|Line Numbers
  1. f = open('csvtab.txt', 'rb')
  2. for item in csv.reader(f, delimiter='\t'):
  3.     print item
Data:
Blues Elwood 1060 W Addison "Chicago, Illinois 60613" B263-1655-2187 116 56

Output:
['Blues', 'Elwood', '1060 W Addison', 'Chicago, Illinois 60613', 'B263-1655-2187', '116', '56']
Sep 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Kylotan | last post: by
10 posts views Thread by Jeff Blaine | last post: by
2 posts views Thread by Mersilla | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.