Package: 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 fromhttps://github.com/roskakori/csv342.InstallationTo install, simply run:$ pip install --upgrade csv342Alternatively you can download the distributin archive fromhttp://pypi.python.org/pypi/csv342/, unpack it and copycsv342.pyinto
your application path.ExamplesFirst, consider changing all string literals in you source code tounicodeinstead ofstrunder Python 2 using to avoid the
messyustring prefix.>>> from __future__ import unicode_literalsThe following examples assume you did that, making your Python 2 code look
like Python 3 even more.Once you importcsv342using>>> import csv342 as csvyour 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 aio.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 aio.StringIOuse:>>> 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))FeaturesSupports Python 2’sunicodestrings.Providesreader,writer,DictReaderandDictWriter.Supports reading and writing with files,io.StringIOetc.Rejects attempts to read or write withcStringIOorStringIO.StringIO(which do not really work withunicode);
useio.StringIOinstead.LimitationsAll limitations of the standardcsvmodule apply.Uses the standardcsv.Snifferunder Python 2.Requires Python 2.6 or later.PerformanceProcessing a CSV with Python 2 usingcsv342is 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.pyOn an ancient laptop with a core 2 duo Intel CPU and Ubuntu 14.04, this takes
10.2 seconds respectively 13.6 seconds.LicenseCopyright (c) 2016-2020, Thomas Aglassinger
All rights reserved.Distributed under the BSD License. For more information, see LICENSE.txt.Version historyVersion 1.0.1, 2020-05-05Fixed inconsistent license information insetup.py(contributed by
Stephen DiCato).Version 1.0.0, 2018-04-02FixedDictReaderso that it can now read from a list of lines
(issue #1, contributed by
Lucas Wiman).FixedAttributeErrorinStringIOtest (contributed by Lucas Wiman).Version 0.2, 2016-04-17Fixed version number when running under Python 3.Fixed helper constantIS_PYHTON2which always wasFalse.Added remaining symbols from Python 2’scsvmodule.Version 0.1, 2016-04-16Initial release.