简体   繁体   English

使用unicode.encode('rot13')时如何修复UnicodeEncodingError

[英]How to fix UnicodeEncodingError when using unicode.encode('rot13')

The version of my python is 2.7. 我的python版本是2.7。

For some reason, I need to use rot13 to convert a paragraph with encoding 'utf-8'. 由于某些原因,我需要使用rot13来转换编码为'utf-8'的段落。 But when I run commands as followed: 但是当我如下运行命令时:

s = u'€'
res = unicode.encode('rot13')

I get the following error message: 我收到以下错误消息:

UnicodeEncodeError: 'charmap' codec can't encode character u'\€' in position 0: character maps to <undefined> . UnicodeEncodeError:'charmap'编解码器无法在位置0编码字符u'\\ u20ac':字符映射到<undefined> How can I fix this error? 如何解决此错误? I was trapped in this error for long time, and can't fix it by the method on Google. 我被这个错误困扰了很长时间,无法通过Google上的方法解决。

The rot13 codec can't do this in Python 2. It's hardcoded to use charmap. rot13编解码器在Python 2中无法做到这一点。它被硬编码为使用charmap。 Switch to Python 3 or just write your own rot13. 切换到Python 3或只编写自己的rot13。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM