473,401 Members | 2,068 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,401 software developers and data experts.

csv module

hello,

I'm using cvs standard module under Python 2.3 / 2.4 to write a file
delimited with tabs. I use the "excel-tab" dialect to do that.

To read my CSV file, I choose to 'sniff' with a sample data in order to
get the dialect.
The problem I meet is that I get a wrong dialect: the sniffer return an
empty string delimiter. It is probably a bug in _guess_delimiter()
method.

The message I obtain is:
TypeError: bad argument type for built-in operation

Do you know a way to sniff tab-delimited data ?
Is it a known bug ?

Bye.

Dec 28 '05 #1
6 1675
Laurent Laporte wrote:
I'm using cvs standard module under Python 2.3 / 2.4 to write a file
delimited with tabs. I use the "excel-tab" dialect to do that.

To read my CSV file, I choose to 'sniff' with a sample data in order to
get the dialect.
The problem I meet is that I get a wrong dialect: the sniffer return an
empty string delimiter. It is probably a bug in _guess_delimiter()
method.

The message I obtain is:
TypeError: bad argument type for built-in operation

Do you know a way to sniff tab-delimited data ?
Is it a known bug ?


http://www.python.org/sf/1157169

</F>

Dec 28 '05 #2

Laurent> To read my CSV file, I choose to 'sniff' with a sample data in
Laurent> order to get the dialect. The problem I meet is that I get a
Laurent> wrong dialect: the sniffer return an empty string delimiter. It
Laurent> is probably a bug in _guess_delimiter() method.

Laurent> The message I obtain is:
Laurent> TypeError: bad argument type for built-in operation

Laurent> Do you know a way to sniff tab-delimited data ?
Laurent> Is it a known bug ?

Using a file with the following contents:
open("tabber.csv", "rb").read() '1\t2\tabc\n3\t4\tdef\n'

I get:
sniffer = csv.Sniffer()
d = sniffer.sniff(open("tabber.csv", "rb").read())
d.delimiter

'\t'

Can you provide a concrete example (preferably in a bug report on SF)?

Skip
Dec 28 '05 #3

me> Using a file with the following contents:

me> >>> open("tabber.csv", "rb").read()
me> '1\t2\tabc\n3\t4\tdef\n'

me> I get:

me> >>> sniffer = csv.Sniffer()
me> >>> d = sniffer.sniff(open("tabber.csv", "rb").read())
me> >>> d.delimiter
me> '\t'

BTW, this also seems to work with a Mac-style EOL:
open("tabber.csv", "rb").read() '1\t2\tabc\r3\t4\tdef\r' d = sniffer.sniff(open("tabber.csv", "rb").read())
d.delimiter

'\t'

Perhaps this has been fixed in CVS.

Skip
Dec 28 '05 #4
Sorry,

Here is my example:

Python 2.3.1 (#1, Sep 29 2003, 15:42:58)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import csv
t_sniffer = csv.Sniffer()
t_data = "aaa\tbbb\r\n\r\nAAA\tBBB\r\n"
t_dialect = t_sniffer.sniff(t_data)
t_dialect.delimiter

''

In fact, I found the pb (thanks to you): I add a newline '\r\n' to
separate the header from the records...

Dec 28 '05 #5
In fact, there is another bug:

In my CVS file, all the records ends with a trailing tab '\t'
except the header because the last field is always empty.

For example, I get :
import csv
t_sniffer = csv.Sniffer()
t_data = "aaa\tbbb\r\nAAA\t\r\nBBB\t\r\n"
t_dialect = t_sniffer.sniff(t_data)
t_dialect.delimiter

''

It is done in the _guess_delimiter() method during the building of
frequency tables. A striping is done for each line (why??)
If I change:
freq = line.strip().count(char)
by:
freq = line.count(char)
It works fine.

Do you have a workaround for that?

------- Laurent.

Dec 28 '05 #6

Laurent> If I change:
Laurent> freq = line.strip().count(char)
Laurent> by:
Laurent> freq = line.count(char)
Laurent> It works fine.

Laurent> Do you have a workaround for that?

Nope. I just checked in precisely your fix to the Python repository.

Skip

Dec 28 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
8
by: Irmen de Jong | last post by:
What would be the best way, if any, to obtain the bytecode for a given loaded module? I can get the source: import inspect import os src = inspect.getsource(os) but there is no...
5
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.