473,405 Members | 2,421 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,405 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 12923
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.