에서 UTF-8로 HTML ASCII가 아닌 데이터를 인코딩하는 방법 나는 그렇게했는데, 나는이 오류를 발견 : 나는 인쇄 문을 사용하는 경우파이썬
>>> import re
>>> x = 'Ingl\xeas'
>>> x
'Ingl\xeas'
>>> print x
Ingl�s
>>> x.decode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 4-5: unexpected end of data
>>> x.decode('utf8', 'ignore')
u'Ingl'
>>> x.decode('utf8', 'replace')
u'Ingl\ufffd'
>>> print x.decode('utf8', 'replace')
Ingl�
>>> print x.decode('utf8', 'xmlcharrefreplace')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: don't know how to handle UnicodeDecodeError in error callback
가, 내가 원하는을 그 :
>>> print x
u'Inglês'
도움이됩니다.
파이썬 3은 파이썬 2와 같이 2 개의 문자열 유형을 가지고 있습니다. 3의'str'은 사소한 수정으로 2의'유니 코드 '입니다. 3의'bytes'는 적당히 수정 된 2의'str'입니다. –
Python 3 예제에서 UnicodeEncodeError 예외가 발생합니다. –
@Daniel : 대화 형 셸에 없습니다. –