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']
が表示されます(リストはそれよりも長くなりますが、そのスタイルで続きます)。2.7 CSVモジュールはユニコードを望んでいますが、ユニコードは不要です
このプログラムはPython 3.3で正常に動作しています。私はそれを3to2で移植しました。私はそうしたように、UNIXからWindowsに切り替えました。
このプログラムをスムーズに書き込むにはどうすればよいですか?
注:このバージョンのcsvモジュールでは、公式のマニュアルに従ってUnicode入力をサポートしていませんが、Unicode入力を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
、私の代わりに
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
あなたは*例外*だけを見ています。 'TypeError'を取り除き、完全なトレースバックを見てください。 –
Python 2.7の 'csv'モジュールは、** unicodeをサポートしない**として文書化されています。 –
'csvfile_'とは何ですか? Unicodeを自動エンコードする 'io'または' codecs'モジュールでオープンされたオープンファイルオブジェクトですか?もしそうなら、それはCSVモジュールではなく、Unicodeを期待する**その**ファイルオブジェクトです。ファイルオブジェクトを開くコードを教えてください。 –