如何在Python3中进行ROT13编码?

Ash*_*ppa 9 rot13 python-3.x

Python 3文档在其编解码器页面上列出了rot13 .

我尝试使用rot13编码对字符串进行编码:

import codecs
s  = "hello"
os = codecs.encode( s, "rot13" )
print(os)
Run Code Online (Sandbox Code Playgroud)

这给出了一个unknown encoding: rot13错误.是否有不同的方法来使用内置的rot13编码?如果在Python 3中删除了这种编码(正如Google搜索结果似乎表明的那样),为什么它仍然在Python3文档中列出?

jfs*_*jfs 17

在Python 3.2+中,有rot_13str-to-str编解码器:

import codecs

print(codecs.encode("hello", "rot-13")) # -> uryyb
Run Code Online (Sandbox Code Playgroud)


and*_*oke 8

啊哈!我以为它已经从Python 3中删除了,但是没有 - 只是接口已经改变了,因为编解码器必须返回字节(这是str-to-str).

这是来自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]
Run Code Online (Sandbox Code Playgroud)