README.rst 2.2 KB

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