>>> help("".encode)
Help on built-in function encode:
encode(...)
S.encode([encoding[,errors]]) -> object
Encodes S using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. **Other possible values are** 'ignore', **'replace'** and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that is able to handle UnicodeEncodeErrors.
Vì vậy, ví dụ:
>>> x
'\xc3\xa9b\xc3\xa1c is the word'
>>> x.decode("ascii")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
>>> x.decode("ascii", "replace")
u'\ufffd\ufffdb\ufffd\ufffdc is the word'
Thêm callback của riêng bạn để codecs.register_error để thay thế với chuỗi của sự lựa chọn của bạn.
Nguồn
2009-12-19 15:45:28
Nếu mã hóa mặc định là ascii, mã hóa nào là chuỗi '' ébác'' trong? –
@Peter Hansen - bạn nói đúng :) nó chỉ là để giải thích những gì tôi muốn ... ví dụ xấu. – olamundo