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

Discussion in 'Python' started by Tim Chase, Jul 19, 2012.

  1. Tim Chase

    Tim Chase Guest

    On 07/19/12 06:21, 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 think I may have stumbled across the "what the heck is happening"
    factor:

    >>> import csv
    >>> from cStringIO import StringIO
    >>> s = StringIO('Email\\\n')
    >>> d = csv.Sniffer().sniff(s.read())
    >>> s.seek(0)
    >>> r = csv.DictReader(s, dialect=d)
    >>> r.fieldnames

    ['Em', 'il']

    It appears that it's finding something repeated [ed: Peter's &
    Steven's replies came in as I finished typing this]. In my first,
    it was the "l" appearing on each line, and in the 2nd example here,
    it's the "a" on each line, so the csv module thinks that's the
    delimiter. The source file comes from an Excel-dialect generation:

    >>> s = StringIO()
    >>> w = csv.writer(s)
    >>> w.writerows([["email"], [""], [""]])
    >>> s.seek(0)
    >>> d = csv.Sniffer().sniff(s.read())
    >>> d.delimiter

    'l'
    >>> s.seek(0)
    >>> r = csv.DictReader(s, dialect=d)
    >>> r.fieldnames

    ['emai', '']


    I guess it then takes the Python community to make the call on
    whether the csv module is doing the right thing in the degenerate
    case. I.e., you can't get back out what you put in when you try to
    sniff.

    -tkc
    Tim Chase, 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:
    510
    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:
    430
    Mahhek
    Apr 21, 2006
  3. Marcelo

    MySql Data Truncation

    Marcelo, Dec 19, 2005, in forum: Java
    Replies:
    3
    Views:
    17,146
    Roedy Green
    Dec 21, 2005
  4. Tim Chase
    Replies:
    7
    Views:
    291
    Dennis Lee Bieber
    Jul 20, 2012
  5. Peter Otten
    Replies:
    0
    Views:
    209
    Peter Otten
    Jul 19, 2012
Loading...

Share This Page