首页 > 解决方案 > 在 Python 3.0 中使用 encoding="utf-8" 加密文本

问题描述

我已经将我的 python 版本从 2 升级到了 3,我用来加密文本文件的程序不再工作了。下面是我的代码片段

for x in range(0, len(content)):
    output = chr(ord(content[x])+ord(key[x])%256)
    crypt = crypt + output

new.write(crypt)

我知道 python 不再使用 ASCII,但新系统似乎有点复杂,难以用于我的目的。有什么方法可以使用 encoding="utf-8" 使我的程序工作而不会过度复杂或过多地更改我的代码?我会很感激所有的答案。以下是我得到的错误

Traceback (most recent call last):
File "C:\Users\Desktop\programing\Python Files\Encryptor and Decryptor\encryption.py", line 26, in <module>
new.write(crypt)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x93' in position 0: character maps to <undefined>

标签: python-3.xencryptionutf-8

解决方案


推荐阅读