您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:UnicodeEncodeError:'latin-1'编解码器无法编码字符

Python:UnicodeEncodeError:'latin-1'编解码器无法编码字符

如果您需要Latin-1编码,则可以使用多种方法来消除破折号或其他255以上的代码点(Latin-1中不包含的字符):

>>> u = u'hello\u2013world'
>>> u.encode('latin-1', 'replace')    # replace it with a question mark
'hello?world'
>>> u.encode('latin-1', 'ignore')     # ignore it
'helloworld'

或自行定制替代品:

>>> u.replace(u'\u2013', '-').encode('latin-1')
'hello-world'

如果不需要输出Latin-1,则UTF-8是常见且首选的选择。W3C推荐它,并且很好地编码了所有Unicode代码点:

>>> u.encode('utf-8')
'hello\xe2\x80\x93world'
python 2022/1/1 18:46:16 有314人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶