Python2's stdlib csv module is nice, but it doesn't support unicode. This module is a drop-in replacement which *does*. If you prefer python 3's semantics but need support in py2, you probably want https://github.com/ryanhiebert/backports.csv
Other
594
stars
87
forks
source link
How can I write code that work in Python 2 and 3? #57
unicodecsv's API differs whether you're on Python 2 or 3, because on Python 3 it delegates to the stdlib. Unfortunately, even ignoring the use of cStringIO, the README example doesn't work in Python 3, because there's no encoding argument to reader (or writer), since the Python 3 csv module just writes strings (unicode).
I'm not sure what the best path forward is to write code that works on both Python 2 and 3. Perhaps it's to write a wrapper that gives an API that works on both. I'm not yet sure. Ideas welcome.
unicodecsv's API differs whether you're on Python 2 or 3, because on Python 3 it delegates to the stdlib. Unfortunately, even ignoring the use of
cStringIO
, the README example doesn't work in Python 3, because there's noencoding
argument to reader (or writer), since the Python 3 csv module just writes strings (unicode).I'm not sure what the best path forward is to write code that works on both Python 2 and 3. Perhaps it's to write a wrapper that gives an API that works on both. I'm not yet sure. Ideas welcome.