csvfile_ = open(finishedFileName+num+".csv","w",newline='')
writ = csv.writer(csvfile_, dialect='excel')
firstline = unicode(str(firstline))
try:
writ.writerow(firstline)
except TypeError:
print firstline
print type(firstline)
raise
我得到了TypeError: must be unicode, not str
这个代码。打印第一行的类型时,我看到<type 'unicode'>
. 当我打印第一行时,我看到['project_number', 'project_location']
(列表比那长,但它继续以这种风格。)
该程序在 python 3.3 中运行良好。我用 3to2 移植它,从 unix 切换到 windows。
如何让这个程序写得流畅?
注意:根据官方文档,这个版本的 csv 模块不支持 Unicode 输入,但它告诉我无论如何都要给它 Unicode 输入。
完全例外
Traceback (most recent call last):
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, in <module>
process(marketingLogExportFileName)
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 123, in process
writing(csvfile,modified,firstline)
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 114, in writing
writ.writerow(firstline)
TypeError: must be unicode, not str
如果我取出代码来制作第一行 unicode,我会得到
Traceback (most recent call last):
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 382, in <module>
process(marketingLogExportFileName)
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 123, in process
writing(csvfile_,modified,firstline)
File "C:\Users\urightswt\Downloads\LogModToConvert.py", line 114, in writing
writ.writerow(firstline)
TypeError: must be unicode, not str