[英]unicode, not str in python
我試圖運行以下Python代碼:
with io.open(outfile, 'w' ) as processed_text, io.open(infile, 'r') as fin:
for line in fin:
processed_text.write(preprocess(line.rstrip())+'\n')
但收到TypeError: must be unicode, not str
我怎么解決這個問題? 我在這里搜索了類似的問題,然后找到了一個可以嘗試的問題
with io.open(outfile, 'w', encoding="utf-8") as processed_text, io.open(infile, 'r') as fin:
但是沒有用
嘗試將其放在文件的最頂部:
from __future__ import unicode_literals
Python 3.x默認使用unicode。 這將導致Python 2.x遵循相同的行為。
如果仍然有問題,您可以手動轉換問題字符串ala
uni_string = unicode(my_string)
使用io.open
打開文件時,請確保編寫unicode
字符串。 這樣的事情應該可以解決問題:
with io.open(outfile, 'w' ) as processed_text, io.open(infile, 'r') as fin:
for line in fin:
s = preprocess(line.rstrip())
if isinstance(s, str):
s = s.decode('utf8')
processed_text.write(s + u'\n')
或修改preprocess
以確保它返回unicode
字符串。
嘗試在已處理的字符串前面寫u,例如[u'blah']
注意事項 :
由於此模塊主要是為Python 3.x設計的,因此必須注意,本文檔中對“字節”的所有使用均指代str類型(其字節為別名),而對“文本”的所有使用均指代。改為unicode類型。 此外,這兩種類型在io API中不可互換。
In [1]: import io
In [2]: def preprocess(s):
...: return bytes(s)
...:
In [3]: with io.open('tst1.out', 'w') as processed_text, io.open('tst1', 'r') as fin:
...: for line in fin:
...: try:
...: out_line = unicode(preprocess(line.rstrip() + '\n'), 'utf-8')
...: except TypeError:
...: out_line = preprocess(line.rstrip() + '\n')
...: processed_text.write(out_line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.