python rot13解密_How to ROT13 encode in Python3?

问题

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值