Skip Montanaro <[EMAIL PROTECTED]> added the comment: Jean-Philippe> The fact is this code is in use in an application where Jean-Philippe> users can submit a .csv file produced by Excel for Jean-Philippe> treatment. The file must contain a "Sequence" column Jean-Philippe> since that is what the treatment is run on. Now I had to Jean-Philippe> make the following changes to my code to account for the Jean-Philippe> fact that some users submit a single column file (since Jean-Philippe> only the "Sequence" column is required for treatment):
Jean-Philippe> f = open(sys.argv[-1], 'r') Jean-Philippe> try: Jean-Philippe> dialect = csv.Sniffer().sniff(f.readline(), [',', '\t']) Jean-Philippe> f.seek(0) Jean-Philippe> reader = csv.DictReader(f, dialect=dialect) Jean-Philippe> except: Jean-Philippe> print '>>>caught csv sniff() exception' Jean-Philippe> f.seek(0) Jean-Philippe> reader = csv.DictReader(f) Jean-Philippe> for line in reader: Jean-Philippe> Do what I need to do What exceptions are you catching? Why are you only giving it a single line of input as a sample? What happens if you instead use f.read(1024) as the sample? When there is only a single column in the file and you give it a delimiter set which doesn't include any characters in the file it (I think correctly) raises an exception to tell you that it couldn't determine the delimiter: >>> import csv >>> f = open("listB2Mforblast.csv") >>> dialect = csv.Sniffer().sniff(f.read(1024)) >>> dialect.delimiter '"' >>> f.seek(0) >>> dialect = csv.Sniffer().sniff(f.read(1024), ",\t :;") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/skip/local/lib/python2.6/csv.py", line 161, in sniff raise Error, "Could not determine delimiter" _csv.Error: Could not determine delimiter In that case, use csv.excel as the dialect. It doesn't matter what you use as the delimiter if it doesn't occur in the file, and if it can't figure out the delimiter it's also not going to guess the quotechar. >>> try: ... dialect = csv.Sniffer().sniff(f.read(1024), ",\t :;") ... except csv.Error: ... dialect = csv.excel ... I personally don't much like the sniffer. It doesn't use any knowledge of the structure of a CSV file to guess the delimiter and quotechar (and those are the only two parameters it does guess). I would prefer if it just went away, but folks use it so it's likely to remain in its current form for the forseeable future. Skip __________________________________ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue2078> __________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com