我一直在使用Python 2.6。当我编写一个Python程序来处理来自SQL Server的查询结果(以CSV格式)时,我发现它不支持Unicode。
当我使用csv文件运行程序时,会弹出一个错误提示:
for row in csvReader:
Error: line contains NULL byte
我使用Ultraedit将csv文件保存为ANSI/ASCII格式后,程序可以正常运行。
我尝试使用编码选项,但失败了:
csvReader = csv.reader(open(fname, mode='rb', encoding='unicode'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function
csvReader = csv.reader(open(fname, mode='rb', encoding='utf-8'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function
我想知道 Python 3 是否支持 Unicode 读取。如果可以,这将节省我很多工作。
open
函数绝对支持encoding=
参数,因此要么您正在使用不同版本的Python,要么您意外地覆盖了open
函数。 - David Wolever