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!!
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: - import csv
-
-
f = open('csv.txt', 'rb')
-
for item in csv.reader(f):
-
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"
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'?
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: - f = open('csvtab.txt', 'rb')
-
for item in csv.reader(f, delimiter='\t'):
-
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']
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |