解码 python rot13 字符串
Decode python rot13 string
我在 python 中有一个 rot13
编码字符串,我必须对其进行解码:
"Gur dhvpx oebja sbk whzcrq bire gur ynml qbt"
可以在python做吗?我找不到任何方法来做到这一点。
请帮忙!
这是我试过的:
s = "Gur dhvpx oebja sbk whzcrq bire gur ynml qbt"
s1 = ""
for i in range(len(s)):
c = s[i]
if c >= 'a' and c <= 'm': c += 13
elif c > 'A' and c < 'M': c += 13
elif c >= 'n' and c < 'z': c -= 13
elif c >= 'N' and c <= 'Z': c -= 13
s1 += c
print(c)
print(s1)
import string #use this package
utf8=string.maketrans("NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm","ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz") #this are the related alphabets
print(string.translate("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt", utf8))
要解码 rot13
编码的字符串,例如 s
,只需再次获取字符串的 rot13
,即计算 rot13(s)
.
如果您不熟悉如何在 python 中计算 rot13
,请尝试使用谷歌搜索,您一定会找到的。我用谷歌搜索并找到了一个非常有效的解决方案: [请注意,它仅适用于 python2 而不是 python3,因为 string.maketrans
已从 python 中删除]3.]
为了完整起见,我将在此处编写代码:
# Python 2 solution
import string
rot13Table = string.maketrans(
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
rot13 = lambda s : string.translate(s, rot13Table)
print rot13("Hello World!")
# Outputs "Uryyb Jbeyq!"
注意 :我不会编写与 OP 最初发布的内容相对应的解码字符串,因为它非常冒犯和恐同。有兴趣的可以自己做。
Update :我还从 this SO answer 中找到了适用于 python 2 和 3 的解决方案。原来在 codecs
模块的 python 中有一个内置的 rot13
编码器:
# Python 2 & 3 compatible
import codecs
rot13 = lambda s : codecs.getencoder("rot-13")(s)[0]
# Answer
rot13("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt")
更新 2 :由于 OP 坚持知道答案并且似乎没有安装 python,我创建了一个 JS 解决方案 https://codepen.io/anon/pen/zPYVQP.其他请谨慎行事。
我在 python 中有一个 rot13
编码字符串,我必须对其进行解码:
"Gur dhvpx oebja sbk whzcrq bire gur ynml qbt"
可以在python做吗?我找不到任何方法来做到这一点。
请帮忙!
这是我试过的:
s = "Gur dhvpx oebja sbk whzcrq bire gur ynml qbt"
s1 = ""
for i in range(len(s)):
c = s[i]
if c >= 'a' and c <= 'm': c += 13
elif c > 'A' and c < 'M': c += 13
elif c >= 'n' and c < 'z': c -= 13
elif c >= 'N' and c <= 'Z': c -= 13
s1 += c
print(c)
print(s1)
import string #use this package
utf8=string.maketrans("NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm","ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz") #this are the related alphabets
print(string.translate("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt", utf8))
要解码 rot13
编码的字符串,例如 s
,只需再次获取字符串的 rot13
,即计算 rot13(s)
.
如果您不熟悉如何在 python 中计算 rot13
,请尝试使用谷歌搜索,您一定会找到的。我用谷歌搜索并找到了一个非常有效的解决方案: [请注意,它仅适用于 python2 而不是 python3,因为 string.maketrans
已从 python 中删除]3.]
为了完整起见,我将在此处编写代码:
# Python 2 solution
import string
rot13Table = string.maketrans(
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
rot13 = lambda s : string.translate(s, rot13Table)
print rot13("Hello World!")
# Outputs "Uryyb Jbeyq!"
注意 :我不会编写与 OP 最初发布的内容相对应的解码字符串,因为它非常冒犯和恐同。有兴趣的可以自己做。
Update :我还从 this SO answer 中找到了适用于 python 2 和 3 的解决方案。原来在 codecs
模块的 python 中有一个内置的 rot13
编码器:
# Python 2 & 3 compatible
import codecs
rot13 = lambda s : codecs.getencoder("rot-13")(s)[0]
# Answer
rot13("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt")
更新 2 :由于 OP 坚持知道答案并且似乎没有安装 python,我创建了一个 JS 解决方案 https://codepen.io/anon/pen/zPYVQP.其他请谨慎行事。