Python 尝试加密和写入文件时出现 UnicodeEncodeError
UnicodeEncodeError in Python when trying to encrypt and write to a file
second post 这里(在同一代码上)。然而这次是一个不同的问题。它只是偶尔发生,但它让我困惑为什么。这是输出和错误:
Phrase to be encrypted: Hello world
Shift keyword, Word only: Hello
:-) :-) :-) Encrypting Phrase 10%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 20%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 30%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 40%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 50%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 60%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 70%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 80%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 90%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 100%... :-) :-) :-)
:-) :-) :-) Checking Security of encrypted phrase.... :-) :-) :-)
:-) :-) :-) Done! :-) :-) :-)
Here is your Encrypted Phrase:I?j!Qgea:~~[
Traceback (most recent call last):
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 60, in <module>
file.write (str(result) + " " + (Cipher))
File "C:\Users\Isaac Scarisbrick\AppData\Local\Programs\Python\Python35-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 characters in position 2-3: character maps to <undefined>
这是我的代码:
import random
phrase = input('Phrase to be encrypted: ')
shift_key = input("Shift keyword, Word only: ")
Encryption_Base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;:<>?'@#~]}[{=+-_)(*&^%$£!`¬\|"
key_encryption = random.randint(0, 94)
Cipher = ''
for c in shift_key:
if c in Encryption_Base:
Cipher += Encryption_Base[(Encryption_Base.index(c)+key_encryption)%(len(Encryption_Base))]
def Keyword_Encryption(key, phrase):
if len(phrase) > len(key):
while len(phrase) > len(key):
length_to_add = len(phrase) - len(key)
key = key + key[0:length_to_add]
elif len(phrase) < len(key):
while len(phrase) < len(key):
length_to_sub = len(key) - (len(key) - len(phrase))
key = key[0:length_to_sub]
else:
pass
shifted_phrase = ''
for i in range(len(phrase)):
new_letter = (ord(key[i]) - 96) + (ord(phrase[i]) - 96) + 96
if new_letter > 1220:
new_letter = chr(new_letter - 26)
else:
new_letter = chr(new_letter)
shifted_phrase = shifted_phrase + new_letter
return shifted_phrase
result = Keyword_Encryption(Cipher, phrase)
print (" ")
print (":-) " * 3 + "Encrypting Phrase 10%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 20%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 30%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 40%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 50%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 60%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 70%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 80%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 90%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 100%... " + ":-) " * 3)
print (":-) " * 3 + "Checking Security of encrypted phrase.... " + ":-) " * 3)
print (":-) " * 3 + "Done! " + ":-) " * 3)
print (" ")
print ('Here is your Encrypted Phrase:' + (result) + (Cipher))
file = open("Encrypted.txt", "w")
file.write (str(result) + " " + (Cipher))
file.close()
提前非常感谢您,因为这是我在 A level class 中设置的任务的一个小扩展。这里有一些代码片段,您之前可能已经看过,因为我从中提取了一些在 python 中制作的加密程序。谢谢你的时间:)。
编辑:如果这有帮助,它有时也会抛出此错误:
Phrase to be encrypted: Hello World
Shift keyword, Word only: Hello
Traceback (most recent call last):
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 42, in <module>
result = Keyword_Encryption(Cipher, phrase)
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 37, in Keyword_Encryption
new_letter = chr(new_letter)
ValueError: chr() arg not in range(0x110000)
好的。所以任何其他查看此 post 的人都知道。您需要做的就是输入
import codecs
在顶部然后当你写文件类型时
encoding = "utf8"
里面写括号。希望我对你的问题有所帮助,感谢所有帮助我得出这个结论的人:-)。
second post 这里(在同一代码上)。然而这次是一个不同的问题。它只是偶尔发生,但它让我困惑为什么。这是输出和错误:
Phrase to be encrypted: Hello world
Shift keyword, Word only: Hello
:-) :-) :-) Encrypting Phrase 10%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 20%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 30%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 40%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 50%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 60%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 70%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 80%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 90%... :-) :-) :-)
:-) :-) :-) Encrypting Phrase 100%... :-) :-) :-)
:-) :-) :-) Checking Security of encrypted phrase.... :-) :-) :-)
:-) :-) :-) Done! :-) :-) :-)
Here is your Encrypted Phrase:I?j!Qgea:~~[
Traceback (most recent call last):
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 60, in <module>
file.write (str(result) + " " + (Cipher))
File "C:\Users\Isaac Scarisbrick\AppData\Local\Programs\Python\Python35-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 characters in position 2-3: character maps to <undefined>
这是我的代码:
import random
phrase = input('Phrase to be encrypted: ')
shift_key = input("Shift keyword, Word only: ")
Encryption_Base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;:<>?'@#~]}[{=+-_)(*&^%$£!`¬\|"
key_encryption = random.randint(0, 94)
Cipher = ''
for c in shift_key:
if c in Encryption_Base:
Cipher += Encryption_Base[(Encryption_Base.index(c)+key_encryption)%(len(Encryption_Base))]
def Keyword_Encryption(key, phrase):
if len(phrase) > len(key):
while len(phrase) > len(key):
length_to_add = len(phrase) - len(key)
key = key + key[0:length_to_add]
elif len(phrase) < len(key):
while len(phrase) < len(key):
length_to_sub = len(key) - (len(key) - len(phrase))
key = key[0:length_to_sub]
else:
pass
shifted_phrase = ''
for i in range(len(phrase)):
new_letter = (ord(key[i]) - 96) + (ord(phrase[i]) - 96) + 96
if new_letter > 1220:
new_letter = chr(new_letter - 26)
else:
new_letter = chr(new_letter)
shifted_phrase = shifted_phrase + new_letter
return shifted_phrase
result = Keyword_Encryption(Cipher, phrase)
print (" ")
print (":-) " * 3 + "Encrypting Phrase 10%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 20%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 30%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 40%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 50%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 60%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 70%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 80%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 90%... " + ":-) " * 3)
print (":-) " * 3 + "Encrypting Phrase 100%... " + ":-) " * 3)
print (":-) " * 3 + "Checking Security of encrypted phrase.... " + ":-) " * 3)
print (":-) " * 3 + "Done! " + ":-) " * 3)
print (" ")
print ('Here is your Encrypted Phrase:' + (result) + (Cipher))
file = open("Encrypted.txt", "w")
file.write (str(result) + " " + (Cipher))
file.close()
提前非常感谢您,因为这是我在 A level class 中设置的任务的一个小扩展。这里有一些代码片段,您之前可能已经看过,因为我从中提取了一些在 python 中制作的加密程序。谢谢你的时间:)。
编辑:如果这有帮助,它有时也会抛出此错误:
Phrase to be encrypted: Hello World
Shift keyword, Word only: Hello
Traceback (most recent call last):
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 42, in <module>
result = Keyword_Encryption(Cipher, phrase)
File "C:\Users\Isaac Scarisbrick\Downloads\Keyword Cipher_1.py", line 37, in Keyword_Encryption
new_letter = chr(new_letter)
ValueError: chr() arg not in range(0x110000)
好的。所以任何其他查看此 post 的人都知道。您需要做的就是输入
import codecs
在顶部然后当你写文件类型时
encoding = "utf8"
里面写括号。希望我对你的问题有所帮助,感谢所有帮助我得出这个结论的人:-)。