How to ROT13 encode in Python3?

大城市里の小女人 提交于 2019-12-17 20:17:50

问题


The Python 3 documentation has rot13 listed on its codecs page.

I tried encoding a string using rot13 encoding:

import codecs
s  = "hello"
os = codecs.encode( s, "rot13" )
print(os)

This gives a unknown encoding: rot13 error. Is there a different way to use the in-built rot13 encoding? If this encoding has been removed in Python 3 (as Google search results seem to indicate), why is it still listed in Python3 documentation?


回答1:


Aha! I thought it had been dropped from Python 3, but no - it is just that the interface has changed, because a codec has to return bytes (and this is str-to-str).

This is from http://www.wefearchange.org/2012/01/python-3-porting-fun-redux.html :

import codecs
s   = "hello"
enc = codecs.getencoder( "rot-13" )
os  = enc( s )[0]



回答2:


In Python 3.2+, there is rot_13 str-to-str codec:

import codecs

print(codecs.encode("hello", "rot-13")) # -> uryyb


来源:https://stackoverflow.com/questions/10576347/how-to-rot13-encode-in-python3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!