Python2's stdlib csv module is nice, but it doesn't support unicode. This module is a drop-in replacement which *does*.
Project description
The unicodecsv is a drop-in replacement for Python 2.7’s csv module which supports unicode strings without a hassle. Supported versions are python 2.7, 3.3, 3.4, 3.5, and pypy 2.4.0.
More fully
Python 2’s csv module doesn’t easily deal with unicode strings, leading to the dreaded “‘ascii’ codec can’t encode characters in position …” exception.
You can work around it by encoding everything just before calling write (or just after read), but why not add support to the serializer?
>>> import unicodecsv as csv
>>> from io import BytesIO
>>> f = BytesIO()
>>> w = csv.writer(f, encoding='utf-8')
>>> _ = w.writerow((u'é', u'ñ'))
>>> _ = f.seek(0)
>>> r = csv.reader(f, encoding='utf-8')
>>> next(r) == [u'é', u'ñ']
True
Note that unicodecsv expects a bytestream, not unicode – so there’s no need to use codecs.open or similar wrappers. Plain open(…, ‘rb’) will do.
(Version 0.14.0 dropped support for python 2.6, but 0.14.1 added it back. See c0b7655248c4249 for the mistaken, breaking change.)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file unicodecsv-0.14.1.tar.gz
.
File metadata
- Download URL: unicodecsv-0.14.1.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 018c08037d48649a0412063ff4eda26eaa81eff1546dbffa51fa5293276ff7fc |
|
MD5 | c18ffe8ded29a4f429224877b2b34252 |
|
BLAKE2b-256 | 6fa4691ab63b17505a26096608cc309960b5a6bdf39e4ba1a793d5f9b1a53270 |