Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Odd csv column-name truncation with only one column

Reply
Thread Tools

Re: Odd csv column-name truncation with only one column

 
 
Peter Otten
Guest
Posts: n/a
 
      07-19-2012
Tim Chase wrote:

> tim@laptop:~/tmp$ python
> Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:4
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import csv
>>>> from cStringIO import StringIO
>>>> s = StringIO('Email\(E-Mail Removed)\(E-Mail Removed)\ n')
>>>> s.seek(0)
>>>> d = csv.Sniffer().sniff(s.read())
>>>> s.seek(0)
>>>> r = csv.DictReader(s, dialect=d)
>>>> r.fieldnames

> ['Emai', '']
>
> I get the same results using Python 3.1.3 (also readily available on
> Debian Stable), as well as working directly on a file rather than a
> StringIO.
>
> Any reason I'm getting ['Emai', ''] (note the missing ell) instead
> of ['Email'] as my resulting fieldnames? Did I miss something in
> the docs?


Judging from

>>> import csv
>>> sniffer = csv.Sniffer()
>>> sniffer.sniff("abc").delimiter

'c'
>>> sniffer.sniff("abc\naba").delimiter

'b'
>>> sniffer.sniff("abc\naba\nxyz").delimiter

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/csv.py", line 184, in sniff
raise Error, "Could not determine delimiter"
_csv.Error: Could not determine delimiter
>>> sniffer.sniff("abc\n"*10 + "xyz").delimiter

'c'
>>> sniffer.sniff("abc\n"*9 + "xyz").delimiter

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/csv.py", line 184, in sniff
raise Error, "Could not determine delimiter"
_csv.Error: Could not determine delimiter

the Sniffer heuristics determines a character that occurs on all of the
first 10 lines to be the delimiter. There are of course examples where that
doesn't make sense to a human observer...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd csv column-name truncation with only one column Tim Chase Python 7 07-20-2012 06:09 PM
Re: Odd csv column-name truncation with only one column Tim Chase Python 0 07-19-2012 01:04 PM
MySql Data Truncation Marcelo Java 3 12-21-2005 01:29 AM
Text Truncation manas ASP .Net 1 07-01-2005 10:41 PM
Truncation of long filename plxjb Computer Information 0 09-28-2004 06:40 AM



Advertisments