如何在 Python3 中进行 ROT13 编码? [英] How to ROT13 encode in Python3?

查看:62
本文介绍了如何在 Python3 中进行 ROT13 编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

导入编解码器s = "你好"os = codecs.encode( s, "rot13" )打印(操作系统)

这给出了一个 unknown encoding: rot13 错误.有没有不同的方式来使用内置的 rot13 编码?如果此编码已在 Python 3 中删除(如 Google 搜索结果所示),为什么它仍列在 Python3 文档中?

解决方案

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

这是来自 http://www.wefearchange.org/2012/01/python-3-porting-fun-redux.html:

导入编解码器s = "你好"enc = codecs.getencoder("rot-13")os = enc( s )[0]

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?

解决方案

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]

这篇关于如何在 Python3 中进行 ROT13 编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆