unicodecsv Library
unicodecsv is a Python 2 library that provides a drop-in replacement for the standard library's `csv` module, adding robust Unicode support. The current version is 0.14.1, released in 2016. This library is effectively abandoned and is no longer maintained, primarily serving legacy Python 2 applications.
Warnings
- breaking unicodecsv is fundamentally incompatible with Python 3's `csv` module. Python 3's `csv` module expects text streams, not byte streams, and handles unicode automatically. Using `unicodecsv` in Python 3 will lead to `TypeError` or incorrect encoding behavior.
- deprecated This library is effectively abandoned and unmaintained since its last release in 2016. Its primary purpose was to address Unicode issues in Python 2's standard `csv` module. Python 3's `csv` module resolved these issues natively, making this library obsolete for modern Python development.
- gotcha unicodecsv is designed *exclusively* for Python 2. Attempting to use it in a Python 3 environment is incorrect and will introduce unnecessary complexity and potential encoding errors. It adds no value in Python 3.
- gotcha When using `unicodecsv` in Python 2, ensure files are opened in binary mode (e.g., `open('filename', 'wb')` or `open('filename', 'rb')`). The library expects byte streams for its internal encoding/decoding mechanism.
Install
-
pip install unicodecsv
Imports
- csv
import unicodecsv as csv
- reader
from unicodecsv import reader
- writer
from unicodecsv import writer
Quickstart
import unicodecsv as csv
import io
# This example demonstrates unicodecsv in a Python 2-like environment.
# In Python 3, the standard `csv` module handles unicode correctly.
# Simulate a file-like object for writing (expecting bytes)
output_buffer = io.BytesIO()
writer = csv.writer(output_buffer, encoding='utf-8')
writer.writerow(['héllø', 'wørld', '😊'])
writer.writerow(['line 2', 'value 2', 'another'])
# Get the byte content written to the buffer
csv_bytes = output_buffer.getvalue()
print("unicodecsv: Written CSV bytes (Python 2 style):")
print(csv_bytes.decode('utf-8')) # Decode for printing in Python 3 console
# Simulate a file-like object for reading
input_buffer = io.BytesIO(csv_bytes)
reader = csv.reader(input_buffer, encoding='utf-8')
print("\nunicodecsv: Read CSV rows (Python 2 style):")
for row in reader:
print(row)
# Expected output in a Python 2 environment would be lists of unicode strings.