How to convert \xXY encoded characters to UTF-8 in Python?
问题 I have a text which contains characters such as "\xaf", "\xbe", which, as I understand it from this question, are ASCII encoded characters. I want to convert them in Python to their UTF-8 equivalents. The usual string.encode("utf-8") throws UnicodeDecodeError . Is there some better way, e.g., with the codecs standard library? Sample 200 characters here. 回答1: Your file is already a UTF-8 encoded file. # saved encoding-sample to /tmp/encoding-sample import codecs fp= codecs.open("/tmp/encoding