473,233 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 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 12883
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

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

Similar topics

7
by: Kylotan | last post by:
I have a text file where the fields are delimited in various different ways. For example, strings are terminated with a tilde, numbers are terminated with whitespace, and some identifiers are...
33
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
10
by: Jeff Blaine | last post by:
It's been a year or so since I written Python code, so maybe I am just doing something really dumb, but... Documentation ============= class DictReader(csvfile]]]]) Create an object...
1
by: vang | last post by:
How do I find out which delimiter if found/used when splitting a string with multiple delimiters are defined in a char array? Example: dim i as integer dim returnText as string dim InputText...
8
by: shorti | last post by:
DB2 V8.2 on AIX I am looking for an efficient way to update several columns in a table that will have a default change. The problem is the table is large (million records) and there are 1 to 4...
1
by: pencoder | last post by:
Actually i wrote C# codings to read text between delimiters.it works nicely. in my codings i have a condition it checks whether the delimiter indexed first time or the second time.based on that i...
2
by: Mersilla | last post by:
Dear All, I am currently new to perl and is experiencing difficulties with splitting and array with multiple conditions. For example , I would like to spilt an element, which contains one or...
1
by: gopiganguly | last post by:
Hi everyone, There is a small problem encountered while creating a package in sql server 2005. Actually i am using a flat file which has 820 rows and 2 columns which are seperated by line...
4
by: monniewolf | last post by:
Hi All, Just a quick question. I have some text files that are space-delimited. Some of the columns in this file have been padded with zeros so that when you open the text file in a simple text...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.