は、この関数を考えてみましょう:私はstr.decode()を行っていますが、PythonはUnicodeEncodeErrorをスローします。どうして?
def escape(text):
print repr(text)
escaped_chars = []
for c in text:
try:
c = c.decode('ascii')
except UnicodeDecodeError:
c = '&{};'.format(htmlentitydefs.codepoint2name[ord(c)])
escaped_chars.append(c)
return ''.join(escaped_chars)
それは、対応するhtmlentitydefsにより、すべての非ASCII文字をエスケープする必要があります。変数text
はそのrepr()
u'Tam\xe1s Horv\xe1th'
ある文字列が含まれている場合、残念ながら、Pythonは
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 0: ordinal not in range(128)
をスローします。
しかし、str.encode()
は使用しません。私はstr.decode()
を使用します。私は何かが恋しいですか?
で解決策を見つけましたか? – Aufwind
または、ナンセンス文字をエスケープする私のアプローチですか? – Aufwind