Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的python文本文件无法工作_Python_Python 3.x - Fatal编程技术网

我的python文本文件无法工作

我的python文本文件无法工作,python,python-3.x,Python,Python 3.x,我看不出这个代码是如何工作的。你能帮忙吗 以下是我目前的代码: import math while True: message = input("Enter a message :") code1 = input("Enter a first code word :") code2 = input("Enter a second code word :") newmessage = "" new = "" listM = [(ord(m.lower

我看不出这个代码是如何工作的。你能帮忙吗

以下是我目前的代码:

import math
while True:
    message = input("Enter a message :")
    code1 = input("Enter a first code word :")
    code2 = input("Enter a second code word :")
    newmessage = ""
    new = ""
    listM = [(ord(m.lower())- 96) for m in message]
    listC1 = [(ord(c1.lower())- 96) for c1 in code1]
    listC2 = [(ord(c2.lower())- 96) for c2 in code2]
    messagec1 = math.ceil(len(listM) / len(listC1))
    messagec2 = math.ceil(len(listM) / len(listC2))
    newlistC1 = listC1 * messagec1
    newlistC2 = listC2 * messagec2
    adding1 = [listM[i] + newlistC1[i] for i in range(0,len(listM))]
    adding2 = [adding1[i] + newlistC2[i] for i in range(0,len(adding1))]
    finallist = [(adding2[i] + 96) for i in range(0,len(adding2))]
    for i in range(0,len(finallist)):
        newmessage += chr(finallist[i])
    print("In the text file called message")
    textfile = open("message.txt","w")
    textfile.write(newmessage)
    textfile.close()
    choice = input("would you like to decrypt aswell? (if you do type yes)")
    if choice == "yes":
        textfile = open("message.txt","r")
        textfileM = textfile.readlines()
        textfile.close
        listM = [(ord(str(m))- 96) for m in textfileM]
        taking1 = [listM[i] - newlistC2[i] for i in range(0,len(listM))]
        taking2 = [taking1[i] - newlistC1[i] for i in range(0,len(listM))]
        decryption = [(taking2[i] + 96) for i in range(0,len(taking2))]
        for i in range(0,len(decryption)):
            new += chr(decryption[i])
        print(new)
当我运行此代码时,它会输出:

Enter a message :hello
Enter a first code word :hi
Enter a second code word :cow
In the text file called message
Traceback (most recent call last):
  File "C:\Users\User\Desktop\python\TASK £ TEXT FILES TEST.py", line 22, in <module>
    textfile.write(newmessage)
  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 '\x8b' in position 2: character maps to <undefined>
输入一条消息:您好
输入第一个码字:hi
输入第二个码字:cow
在名为message的文本文件中
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Desktop\python\TASK£TEXT FILES TEST.py”,第22行,在
textfile.write(newmessage)
文件“C:\Python34\lib\encodings\cp1252.py”,第19行,在encode中
返回codecs.charmap\u encode(输入、自身错误、编码表)[0]
UnicodeEncodeError:“charmap”编解码器无法对位置2中的字符“\x8b”进行编码:字符映射到

尝试使用编解码器库打开文件。它应该会起作用

import codecs

f = codecs.open(filename, 'w', 'utf-8')
f.write(your_data)
您还可以将其设置为该模块的“全局”配置:

import sys
reload(sys)  
sys.setdefaultencoding('utf-8')
然后在阅读时阅读文件


我还建议您阅读这篇文章:

看起来您的输入文件中有一些非ASCII文本,不是吗?我需要什么来让它工作?什么是编解码器?对不起,我对蟒蛇还不熟悉。我可以把它放在哪里?开始时?打开文件时