Giter Club home page Giter Club logo

csv342's Introduction

csv342

csv342 is a Python module similar to the the csv module in the standard library. Under Python 3, it just calls the standard csv module. Under Python 2, it provides a Python 3 like interface to reading and writing CSV files, in particular concerning non ASCII characters.

It is distributed under the BSD license. The source code is available from https://github.com/roskakori/csv342.

Installation

To install, simply run:

$ pip install --upgrade csv342

Alternatively you can download the distributin archive from http://pypi.python.org/pypi/csv342/, unpack it and copy csv342.py into your application path.

Examples

First, consider changing all string literals in you source code to unicode instead of str under Python 2 using to avoid the messy u string prefix.

>>> from __future__ import unicode_literals

The following examples assume you did that, making your Python 2 code look like Python 3 even more.

Once you import csv342 using

>>> import csv342 as csv

your code can call CSV functions the same way independent of whether it runs under Python 2 or 3. First, let's write a few test data to a io.StringIO:

>>> import io
>>> csv_stream = io.StringIO()
>>> csv_writer = csv.writer(csv_stream)
>>> csv_writer.writerow(['a', 'b'])
>>> csv_writer.writerow(['1', '"x"'])
>>> csv_content = csv_stream.getvalue()
>>> str(csv_content)
'a,b\r\n1,"""x"""\r\n'

To read data from a io.StringIO use:

>>> csv_stream = io.StringIO('a,b\r\n1,"""x"""\r\n')
>>> csv_reader = csv.reader(csv_stream)
>>> for row in csv_reader:
>>>     print(row)

To read a UTF-8 encoded CSV file with non ASCII characters use:

>>> csv_path = os.path.join('test', 'utf-8.csv')
>>> with io.open(csv_path, encoding='utf-8', newline='') as csv_file:
>>>     csv_reader = csv.reader(csv_file, delimiter=',')
>>>     for row in csv_reader:
>>>         print('row {0:d}: data={1}'.format(csv_reader.line_num, row))

Features

  • Supports Python 2's unicode strings.
  • Provides reader, writer, DictReader and DictWriter.
  • Supports reading and writing with files, io.StringIO etc.
  • Rejects attempts to read or write with cStringIO or StringIO.StringIO (which do not really work with unicode); use io.StringIO instead.

Limitations

  • All limitations of the standard csv module apply.
  • Uses the standard csv.Sniffer under Python 2.
  • Requires Python 2.6 or later.

Performance

Processing a CSV with Python 2 using csv342 is about 30% slower than processing it with Python 3. This is probably due the fact that under Python 2 there is an intermediate translation to UTF-8 using pure Python code while in Python 3 uses mostly native code.

Provided you have both Python 2 and 3 installed on the same machine, you can test this yourself running:

python3 test/performance.py
python2 test/performance.py

On an ancient laptop with a core 2 duo Intel CPU and Ubuntu 14.04, this takes 10.2 seconds respectively 13.6 seconds.

License

Copyright (c) 2016-2020, Thomas Aglassinger All rights reserved.

Distributed under the BSD License. For more information, see LICENSE.txt.

Version history

Version 1.0.1, 2020-05-05

  • Fixed inconsistent license information in setup.py (contributed by Stephen DiCato).

Version 1.0.0, 2018-04-02

  • Fixed DictReader so that it can now read from a list of lines (issue #1, contributed by Lucas Wiman).
  • Fixed AttributeError in StringIO test (contributed by Lucas Wiman).

Version 0.2, 2016-04-17

  • Fixed version number when running under Python 3.
  • Fixed helper constant IS_PYHTON2 which always was False.
  • Added remaining symbols from Python 2's csv module.

Version 0.1, 2016-04-16

  • Initial release.

csv342's People

Contributors

dicato avatar lucaswiman avatar roskakori avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

percipient

csv342's Issues

DictReader has a different interface from csv.DictReader

The csv342 version of DictReader cannot accept a list of CSV lines, though the built in version in both Python 2 and Python 3 can do so. If you're interested in accepting patches, I could submit one for this issue.

csv342 on python 2.7

>>> import csv342 as csv
>>> list(csv.DictReader(['a,b', '1,2']))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 239, in next
    return self.__next__()
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 220, in __next__
    if self.fieldnames is None:
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 205, in fieldnames
    self._set_fieldnames_from_first_row()
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 199, in _set_fieldnames_from_first_row
    self._fieldnames = next(self.reader)
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 161, in next
    return self.__next__()
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 156, in __next__
    row = self.reader.next()
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 140, in next
    return self.__next__()
  File "/Users/lucaswiman/website/vendor/venv/lib/python2.7/site-packages/csv342.py", line 137, in __next__
    return self._text_stream.next().encode('utf-8')
AttributeError: 'list' object has no attribute 'next'

Python 3.4 (also works on 3.6)

>>> import csv
>>> list(csv.DictReader(['a,b', '1,2']))
[{'a': '1', 'b': '2'}]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.