Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25622 > unrolled thread

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

Started byTim Chase <python.list@tim.thechases.com>
First post2012-07-19 08:04 -0500
Last post2012-07-19 08:04 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Odd csv column-name truncation with only one column Tim Chase <python.list@tim.thechases.com> - 2012-07-19 08:04 -0500

#25622 — Re: Odd csv column-name truncation with only one column

FromTim Chase <python.list@tim.thechases.com>
Date2012-07-19 08:04 -0500
SubjectRe: Odd csv column-name truncation with only one column
Message-ID<mailman.2299.1342703031.4697.python-list@python.org>
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\nfoo@example.com\nbar@example.org\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\nfoo@example.org\nbar@test.test\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"], ["foo@example.com"], ["bar@example.org"]])
>>> 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



[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web