Backport of Python 3 csv module
The API of the csv module in Python 2 is drastically different from the csv module in Python 3. This is due, for the most part, to the difference between str in Python 2 and Python 3.
The semantics of Python 3’s version are more useful because they support unicode natively, while Python 2’s csv does not.
pip install backports.csv
First make sure you’re starting your file off right:
from backports import csv
Then be careful with your files to handle the encoding. If you’re working with a binary file-like object, io.TextIOWrapper can be very helpful. If you’re dealing with a file, you can just use io.open instead of Python 2’s open builtin, and it works just like Python 3’s builtin open.
from backports import csv import io def read_csv(filename): with io.open(filename, newline='', encoding='utf-8') as f: for row in csv.reader(f): yield row def write_csv(filename, rows): with io.open(filename, 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) for row in rows: writer.writerow(row)
Note: It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.
- Add tests to MANIFEST.in. - thanks to @jayvdb for the pull request
- Pass reader error messages along. (#28) This should help make errors more transparent. - thanks to @mpeteuil for the pull request
- Fix bug in README example. (#22) - thanks to @tantale for the bug report
- Allow None as quotechar when using QUOTE_NONE. (#23) - thanks to @thanatos for the bug report
- Return write value from writerow. (#20) - thanks to @therg
- Add LICENSE file (#18).
- Avoid quoting any numeric types when using QUOTE_NONNUMERIC. - thanks to @torfsen for the bug report
- Better error messages for invalid dialects. - thanks to @kengruven for the bug report
- Initial Release
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size backports.csv-1.0.7-py2.py3-none-any.whl (12.9 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size backports.csv-1.0.7.tar.gz (20.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for backports.csv-1.0.7-py2.py3-none-any.whl