[issue2078] CSV Sniffer does not function properly on single column .csv files

Jean-Philippe Laverdure report at bugs.python.org
Thu Mar 27 16:19:38 CET 2008


Jean-Philippe Laverdure <jp.laverdure at gmail.com> added the comment:

Hello and sorry for the late reply.

Wolfgang: sorry about my misuse of the csv.DictReader constructor, that 
was a mistake on my part. However, it still is not functionning as I
think it should/could.  Look at this:

Using this content:
Sequence
AAGINRDSL
AAIANHQVL

and this piece of code:
f = open(sys.argv[-1], 'r')
dialect = csv.Sniffer().sniff(f.readline())
f.seek(0)
reader = csv.DictReader(f, dialect=dialect)
for line in reader:
    print line

I get this result:
{'Sequen': 'AAGINRDSL', 'e': None}
{'Sequen': 'AAIANHQVL', 'e': None}

When I really should be getting this:
{'Sequence': 'AAGINRDSL'}
{'Sequence': 'AAIANHQVL'}

The fact is this code is in use in an application where users can submit
a .csv file produced by Excel for treatment.  The file must contain a
"Sequence" column since that is what the treatment is run on. Now I had
to make the following changes to my code to account for the fact that
some users submit a single column file (since only the "Sequence" column
is required for treatment):

f = open(sys.argv[-1], 'r')
try:
    dialect = csv.Sniffer().sniff(f.readline(), [',', '\t'])
    f.seek(0)
    reader = csv.DictReader(f, dialect=dialect)
except:
    print '>>>caught csv sniff() exception'
    f.seek(0)
    reader = csv.DictReader(f)
for line in reader:
    Do what I need to do

Which really feels like a patched use of a buggy implementation of the
Sniffer class

I understand the issues raised by Skip in regards to figuring out a
delimiter at all costs...  But really, the Sniffer class should work
apropriately when a single column .csv file is submitted

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2078>
__________________________________


More information about the Python-bugs-list mailing list