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


Groups > comp.lang.python > #25619

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python': 0.09; '"could': 0.09; 'delimiter': 0.09; 'determines': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stringio': 0.09; 'resulting': 0.13; 'dec': 0.15; "'b'": 0.16; "'c'": 0.16; '(also': 0.16; '(note': 0.16; '3.1.3': 0.16; 'cstringio': 0.16; 'csv': 0.16; 'message- id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; 'examples': 0.18; 'tim': 0.18; '>>>': 0.18; 'import': 0.21; '"",': 0.22; 'occurs': 0.22; 'raise': 0.24; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; "doesn't": 0.28; 'header:X -Complaints-To:1': 0.28; 'lines': 0.28; '>>>>': 0.29; 'chase': 0.29; 'character': 0.29; "i'm": 0.29; 'sense': 0.31; 'file': 0.32; 'could': 0.32; 'getting': 0.33; 'skip:s 30': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'subject:with': 0.36; 'rather': 0.37; 'subject:: ': 0.38; '2010,': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'first': 0.61; 'more': 0.63; 'results': 0.65; 'miss': 0.75
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Odd csv column-name truncation with only one column
Date Thu, 19 Jul 2012 13:49:53 +0200
Organization None
References <5007EDD6.5020903@tim.thechases.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ac5b.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2298.1342698598.4697.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1342698598 news.xs4all.nl 6853 [2001:888:2000:d::a6]:54912
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25619

Show key headers only | View raw


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 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...

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Odd csv column-name truncation with only one column Peter Otten <__peter__@web.de> - 2012-07-19 13:49 +0200

csiph-web