title
テキスト フィールドとを含むクラス チャンクがありますtext
。それらを印刷したいとき、私は (驚き、驚き!) を得UnicodeDecodeError
ます。出力文字列をフォーマットしようとするとエラーが発生しますが、テキストとタイトルを連結して返すと、エラーは発生しません。
class Chunk:
# init, fields, ...
# this implementation will give me an error
def __str__( self ):
return u'{0} {1}'.format ( enc(self.text), enc(self.title) )
# but this is OK - all is printed without error
def __str__( self ):
return enc(self.text) + enc(self.title)
def enc(x):
return x.encode('utf-8','ignore') # tried many combinations of arguments...
c = Chunk()
c.text, c.title = ... # feed from external file
print c
バム!エラー!
return u'{0} {1}'.format ( enc(self.text), enc(self.title) )
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 2844: ordinal not in range(128)
encode
/ decode
/ utf-8
/ ascii
/ replace
/ ignore
/...のすべての可能な組み合わせを使用したと思います。
(Python の Unicode の問題は本当にイライラします!)