[英]Why did python-3.x remove ROT-13 as an encoding?
使用python-2.7,您可以非常輕松地使用rot-13 Ceasar Cipher
>>> 'abcdefghijklmnopqrstuvwxyz'.encode('rot-13')
'nopqrstuvwxyzabcdefghijklm'
您甚至可以在CPython存儲庫的Python of Zen代碼中找到它。
但是,python3.6上的相同代碼給出了 -
>>> 'abcdefghijklmnopqrstuvwxyz'.encode('rot-13')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: 'rot-13' is not a text encoding; use codecs.encode() to handle arbitrary codecs
如果我想在python3.x中使用rot-13
編碼,我需要導入codecs
。
>>> import codecs
>>> codecs.encode('abcdefghijklmnopqrstuvwxyz', 'rot-13')
'nopqrstuvwxyzabcdefghijklm'
當然,這實際上是一個小問題,我不介意導入codecs
來實現凱撒密碼(無論如何它都是內置的)。 我只是想知道這個設計決定背后是否有任何潛在的理由。 也許原因就像“rot-13不是真正的編碼”一樣簡單,我不知道。
如果有人可以對此有所了解,我很樂意聽到它!
您還可以查看此頁面 ,其中有人將您的問題標記為錯誤。 對於那些不想瀏覽網站及其后續鏈接的人來說,python提交者的簡單響應如下:
“由於rot_13是一個代碼轉換器,而不是一個編碼器,錯誤信息是正確的,正如函數的修復一樣。但是,由於模塊中的模塊編碼.rot_13和rot13函數都沒有記錄,(甚至在2.7中都沒有) ,我想知道函數和if __name__;子句是否應該被刪除。“
快速搜索“python rot-13中的新功能”會出現這種情況: https : //docs.python.org/3/whatsnew/3.4.html#codec-handling-improvements
在Python 3.4中,解釋器能夠識別標准庫中提供的已知非文本編碼,並在適當時將用戶引導至這些通用便利功能:
>>> >>> b"abcdef".decode("hex") Traceback (most recent call last): File "<stdin>", line 1, in <module> LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs >>> "hello".encode("rot13") Traceback (most recent call last): File "<stdin>", line 1, in <module> LookupError: 'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs
顯然,這是一個清理操作,用於將實際文本編碼(您將在open(file, encoding="foo")
調用中使用)與其他編碼分開。
Python將rot13移動到(如你所說)編解碼器。 我的猜測是更好地反映rot13是什么,並使用不同的更通用的界面。 正如TimPietzcker所說,很可能是清理並試圖將類似的功能分組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.