realtime output and csv files

Tim Chase python.list at
Sat Feb 6 07:45:53 EST 2016

On 2016-02-06 02:53, Bernardo Sulzbach wrote:
>> And even if you have things to escape or format correctly, the
>> stdlib has a "csv" module that makes this trivially easy:
> I supposed it had one. Obviously, I've never used it myself,
> otherwise I would be sure about its existence. Nice to know about
> it, although I assume that for many data transfers it will not be
> painless if you need several escapes, when CSV starts to become
> complicated it starts to fail because of quirky and naive parsers.

If you read up on the csv module's help, it has all manner of knobs
to twiddle when it comes to the "dialect" of CSV:

- delimiters:  comma is the default, but you can specify others like
  tab or pipe)

- quote character:  double-quote is the default, but you
  can use single-quote or otherwise

- escaping: doubling up the quote-character is the default but I think
  you can specify other schemes

There are others, but that's what I have to tweak most often.

Finally, there's a sniffer in the module, so even if you don't know
the format, you can make an intelligent guess about it:

  with open('example.csv', 'rb') as csvfile:
    dialect = csv.Sniffer().sniff(*4))
    reader = csv.reader(csvfile, dialect)

The only edge case I've hit regularly is that it doesn't (didn't? not
sure if it's been fixed) do the right thing on CSV files containing a
single column, as it would try to accept a common alphanumeric
character as the delimiter instead of making the better assumption
that it was a single column.


More information about the Python-list mailing list