Python 3 文档在其编解码器页面上列出了 rot13 。
我尝试使用 rot13 编码对字符串进行编码:
import codecs
s = "hello"
os = codecs.encode( s, "rot13" )
print(os)
这给出了一个unknown encoding: rot13错误。有没有不同的方法来使用内置的 rot13 编码?如果在 Python 3 中删除了这种编码(如 Google 搜索结果所示),为什么它仍然列在 Python3 文档中?
Python 3 文档在其编解码器页面上列出了 rot13 。
我尝试使用 rot13 编码对字符串进行编码:
import codecs
s = "hello"
os = codecs.encode( s, "rot13" )
print(os)
这给出了一个unknown encoding: rot13错误。有没有不同的方法来使用内置的 rot13 编码?如果在 Python 3 中删除了这种编码(如 Google 搜索结果所示),为什么它仍然列在 Python3 文档中?
在 Python 3.2+ 中,有rot_13str-to-str 编解码器:
import codecs
print(codecs.encode("hello", "rot-13")) # -> uryyb
啊哈!我认为它已从 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]
rot_13在 Python 3.0 中被删除,然后在 v3.2 中重新添加。rot13在 v3.4 中重新添加。
codecs.encode( s, "rot13" )在 Python 3.4+ 中运行良好
实际上,现在你可以使用从现在到现在的任何标点符号rot,13包括:
rot-13, rot@13,rot#13等
https://docs.python.org/3/library/codecs.html#text-transforms
3.2 版中的新功能: rot_13 文本转换的恢复。
在 3.4 版更改: rot13 别名的恢复。
def rot13(message):
Rot13=''
alphabit = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in message:
if i in alphabit:
Rot13 += alphabit[alphabit.index(i) + 13]
else:
Rot13 += i
return Rot13
代码很大,但我只是在学习
首先你需要安装 python 库 - https://pypi.org/project/endecrypt/
pip install endecrypt (windows)
pip3 install endecrypt (linux)
然后,
from endecrypt import cipher
message_to_encode = "Hello World"
conversion = 'rot13conversion'
cipher.encode(message_to_encode, conversion )
# Uryyb Jbeyq
message_to_decode = "Uryyb Jbeyq"
cipher.decode(message_to_decode, conversion)
# Hello World