README.rst
1================================================
2backports.csv: Backport of Python 3's csv module
3================================================
4
5.. image:: https://img.shields.io/pypi/v/backports.csv.svg
6 :target: https://pypi.python.org/pypi/backports.csv
7 :alt: Latest Version
8
9.. image:: https://travis-ci.org/ryanhiebert/backports.csv.svg?branch=master
10 :target: https://travis-ci.org/ryanhiebert/backports.csv
11
12.. image:: https://badges.gitter.im/ryanhiebert/backports.csv.svg
13 :alt: Join the chat at https://gitter.im/ryanhiebert/backports.csv
14 :target: https://gitter.im/ryanhiebert/backports.csv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
15
16.. image:: https://requires.io/github/ryanhiebert/backports.csv/requirements.svg?branch=master
17 :target: https://requires.io/github/ryanhiebert/backports.csv/requirements/?branch=master
18 :alt: Requirements Status
19
20The API of the csv module in Python 2 is drastically different from
21the csv module in Python 3. This is due, for the most part, to the
22difference between str in Python 2 and Python 3.
23
24The semantics of Python 3's version are more useful because they support
25unicode natively, while Python 2's csv does not.
26
27Installation
28============
29
30.. code-block:: sh
31
32 pip install backports.csv
33
34Usage
35=====
36
37First make sure you're starting your file off right:
38
39.. code-block:: python
40
41 from backports import csv
42
43
44Then be careful with your files to handle the encoding.
45If you're working with a binary file-like object,
46``io.TextIOWrapper`` can be very helpful.
47If you're dealing with a file, you can just use ``io.open``
48instead of Python 2's ``open`` builtin, and it works
49just like Python 3's builtin ``open``.
50
51.. code-block:: python
52
53 from backports import csv
54 import io
55
56 def read_csv(filename):
57 with io.open(filename, newline='', encoding='utf-8') as f:
58 for row in csv.reader(f):
59 yield row
60
61 def write_csv(filename, rows):
62 with io.open(filename, 'w', newline='', encoding='utf-8') as f:
63 writer = csv.writer(f)
64 for row in rows:
65 writer.writerow(row)
66
67Note: It should always be safe to specify ``newline=''``,
68since the csv module does its own (universal) newline handling.
69