【发布时间】:2015-09-20 00:58:21
【问题描述】:
所以我试图将一些密文保存到用户命名的新文本文件中,但是当我运行代码时,它会显示以下消息:
Please enter the name you wish the file to be called: cipher
Traceback (most recent call last):
File "C:/Users/User/Documents/file figure.py", line 19, in <module>
f.write(cipher_text_write)
File "C:\Python34\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 '\x8e' in position 1: character maps to <undefined>
我设法弄清楚是我要保存的实际消息导致了问题。任何帮助将不胜感激!
这是我的代码:
cipher_text = " «²²µ ³¿ ´§³« ¯¹ µ´«²² "
filename = input("Please enter the name you wish the file to be called: ")
cipher_text_write = str(cipher_text)
cipher_filename = filename + ".txt"
f = open(cipher_filename,"w+")
f.write(cipher_text_write)
f.close()
【问题讨论】:
-
虽然错误相同,但根本原因和解决方法不同。我不认为这是重复的。
标签: python