python 2的类似python 3的csv模块
csv342的Python项目详细描述
csv342是一个python模块,类似于标准库中的csv模块。在python 3下,它只调用标准csv模块。在
python 2下,它提供了一个类似python 3的接口,用于读取和写入csv
文件,特别是有关非ascii字符的文件。
源代码可从
https://github.com/roskakori/csv342获得。
installation
----
>若要安装,只需运行::
$pip install--升级csv342
将其解包并将“csv342.py”复制到应用程序路径中。
从“导入Unicode文本”开始,以下示例假设您这样做了,使Python2代码看起来更像Python3。
首先,让我们将一些测试数据写入到“io.stringio”中:
>>;import io
>>;>;import io
>>;>;csv-stream=io.stringio()
>>;csv-writer=csv.writer.writer.writer(csv-stream)
>>>>>>gt;csv-writer.writerow(['a','b'])
>>gt;csv-writerow.writerow(['a','b'''>>>>>>>;>;
>;
>>>>>
>>><>>csv_content=csv_stream.getvalue()
>;>>gt;str(csv-content)'a,b\r\n1,“”x“”“\r\n'
``io.stringio``若要从a``io.stringio``中读取数据,请使用:
>>;gt;csv-stream=io.stringio('a,b\r\n1,“”x“”“\r\n’”\r\n')
>>>gt;csv-reader=csv.reader(csv-stream)
>>>>gt;对于csv-reader中的行,csv-reader中的csv-reader=csv.reader(csv>;打印(行)
若要读取包含非ascii字符的utf-8编码csv文件,请使用:
>;>;csv路径=os.path.join('test','utf-8.csv')
>;>;IO.open(csv路径,encoding='utf-8',newline='')作为csv文件:
>;>;csv读卡器=csv.reader(csv文件,分隔符=',')
>;>;对于csv读取器中的行:
>;>print('row{0:d}:data={1}'。format(csv读者行数,row))
features
--
*支持python 2的“unicode”字符串。
*提供“reader`”、“writer`”、“dictreader`”和“dictwriter`”。
*支持用文件进行读写,`` io.stringio``etc.
*拒绝尝试使用``cstringio``或
``stringio.stringio````进行读或写操作(这些操作实际上不适用于``unicode``);
改为使用“io.stringio”。
比用python 3处理它慢。这可能是因为在python
2下,使用纯python代码有一个到utf-8的中间转换,而在python 3中
主要使用本机代码。
您可以自己运行:
python3 test/performance.py
python2 test/performance.py
Thomas Aglasinger
保留所有权利。
根据BSD许可证分发。有关详细信息,请参阅license.txt。
行
(`issue 1<;https://github.com/roskakori/csv342/issues/1>;`,由
lucas wiman提供)。
*修复了``stringio``测试中的``attributeerror`(由lucas wiman提供)。
0.2版,2016-04-17
*在python 3下运行时的固定版本号。
*固定帮助程序常量'is_pyhton2``始终为'false`。
*添加了python 2的'csv``模块中的剩余符号。
0.1版,2016-04-16
*初始版本。
python 2下,它提供了一个类似python 3的接口,用于读取和写入csv
文件,特别是有关非ascii字符的文件。
源代码可从
https://github.com/roskakori/csv342获得。
installation
----
>若要安装,只需运行::
$pip install--升级csv342
将其解包并将“csv342.py”复制到应用程序路径中。
从“导入Unicode文本”开始,以下示例假设您这样做了,使Python2代码看起来更像Python3。
首先,让我们将一些测试数据写入到“io.stringio”中:
>>;import io
>>;>;import io
>>;>;csv-stream=io.stringio()
>>;csv-writer=csv.writer.writer.writer(csv-stream)
>>>>>>gt;csv-writer.writerow(['a','b'])
>>gt;csv-writerow.writerow(['a','b'''>>>>>>>;>;
>;
>>>>>
>>><>>csv_content=csv_stream.getvalue()
>;>>gt;str(csv-content)'a,b\r\n1,“”x“”“\r\n'
``io.stringio``若要从a``io.stringio``中读取数据,请使用:
>>;gt;csv-stream=io.stringio('a,b\r\n1,“”x“”“\r\n’”\r\n')
>>>gt;csv-reader=csv.reader(csv-stream)
>>>>gt;对于csv-reader中的行,csv-reader中的csv-reader=csv.reader(csv>;打印(行)
若要读取包含非ascii字符的utf-8编码csv文件,请使用:
>;>;csv路径=os.path.join('test','utf-8.csv')
>;>;IO.open(csv路径,encoding='utf-8',newline='')作为csv文件:
>;>;csv读卡器=csv.reader(csv文件,分隔符=',')
>;>;对于csv读取器中的行:
>;>print('row{0:d}:data={1}'。format(csv读者行数,row))
features
--
*支持python 2的“unicode”字符串。
*提供“reader`”、“writer`”、“dictreader`”和“dictwriter`”。
*支持用文件进行读写,`` io.stringio``etc.
*拒绝尝试使用``cstringio``或
``stringio.stringio````进行读或写操作(这些操作实际上不适用于``unicode``);
改为使用“io.stringio”。
比用python 3处理它慢。这可能是因为在python
2下,使用纯python代码有一个到utf-8的中间转换,而在python 3中
主要使用本机代码。
您可以自己运行:
python3 test/performance.py
python2 test/performance.py
Thomas Aglasinger
保留所有权利。
根据BSD许可证分发。有关详细信息,请参阅license.txt。
行
(`issue 1<;https://github.com/roskakori/csv342/issues/1>;`,由
lucas wiman提供)。
*修复了``stringio``测试中的``attributeerror`(由lucas wiman提供)。
0.2版,2016-04-17
*在python 3下运行时的固定版本号。
*固定帮助程序常量'is_pyhton2``始终为'false`。
*添加了python 2的'csv``模块中的剩余符号。
0.1版,2016-04-16
*初始版本。