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

Discussion in 'Python' started by Peter Otten, Jul 19, 2012.

  1. Peter Otten

    Peter Otten Guest

    Tim Chase wrote:

    > tim@laptop:~/tmp$ python
    > Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
    > [GCC 4.4.5] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> import csv
    >>>> from cStringIO import StringIO
    >>>> s = StringIO('Email\\\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...
     
    Peter Otten, Jul 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. manas

    Text Truncation

    manas, Jul 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    523
    Mark Fitzpatrick
    Jul 1, 2005
  2. newsgroups.comcast.net

    Odd error from one ip address only

    newsgroups.comcast.net, Apr 5, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    441
    Mahhek
    Apr 21, 2006
  3. Marcelo

    MySql Data Truncation

    Marcelo, Dec 19, 2005, in forum: Java
    Replies:
    3
    Views:
    17,173
    Roedy Green
    Dec 21, 2005
  4. Tim Chase
    Replies:
    7
    Views:
    303
    Dennis Lee Bieber
    Jul 20, 2012
  5. Tim Chase
    Replies:
    0
    Views:
    157
    Tim Chase
    Jul 19, 2012
Loading...

Share This Page