Python 我正在尝试将程序写入文件
这是我的Python 我正在尝试将程序写入文件,python,python-2.7,Python,Python 2.7,这是我的 """ Author: Michael Wellman (wellmanm) Title: pa10.py Description: Deciphering a document using pennymath. """ def decode(inputfile,outputfile): **inputfile = open("superDuperTopSecretStudyGuide.txt","rU").read() outputfile = open("tr
"""
Author: Michael Wellman (wellmanm)
Title: pa10.py
Description: Deciphering a document using pennymath.
"""
def decode(inputfile,outputfile):
**inputfile = open("superDuperTopSecretStudyGuide.txt","rU").read()
outputfile = open("translatedguide.txt","w")**
count = 0
aList = []
for words in inputfile:
aList.append(words)
charCount = len(aList)
**outpufile.write(aList)**
while count<charCount:
print aList[count],
if (aList[count].isalpha()):
if (ord(aList[count])>90):
count = count + ord(aList[count])-95
else:
count = count + ord(aList[count])-63
else:
if (aList[count].isdigit()):
count = count + ord(aList[count])-46
else:
count = count + 6
**inputfile.close()
outputfile.close()**
“”“
作者:迈克尔·韦尔曼(韦尔曼)
标题:pa10.py
描述:使用pennymath破译文档。
"""
def解码(输入文件、输出文件):
**inputfile=open(“superDuperTopSecretStudyGuide.txt”,“rU”).read()
outputfile=open(“translateguide.txt”、“w”)**
计数=0
aList=[]
对于inputfile中的单词:
aList.append(单词)
charCount=len(列表)
**输出文件。写入(列表)**
而(90):
计数=计数+ord(列表[计数])-95
其他:
计数=计数+命令(列表[计数])-63
其他:
如果(aList[count].isdigit()):
计数=计数+命令(列表[计数])-46
其他:
计数=计数+6
**inputfile.close()
outputfile.close()**
txt文件来自我的教授:p
我相信黑体部分是最重要的
有什么想法吗?你试过运行代码吗 我想如果你愿意,你会收到一条关于线路的错误信息
outpufile.write(aList)
请参阅,file.write()
方法的文档字符串清楚地说明:
写入(str)->无。将字符串str写入文件
请注意,由于缓冲的原因,之前可能需要使用flush()或close()
磁盘上的文件反映了写入的数据
您提供的是一个列表
,而不是str
。试着把它改成
outpufile.write(''.join(aList))
或
或者任何适合你需要的。而且,您永远不会清除列表,因此,当您在inputfile
上迭代时,您将写入第一个字符,然后是第一个和第二个字符,然后是前三个字符,等等。这是有意的吗
最后,您试图关闭inputfile
,它实际上是一个str
,而不是一个文件,因为file.read()
方法返回一个str
请注意,如果变量是字符串,请不要调用变量
inputfile
;如果变量是单个字符,请不要调用words
。这对任何人都没有帮助。您是在寻求一般性反馈,还是有问题要解决?如果你想得到一般性的反馈,这个网站会更适合这类问题。你问的唯一问题是“有什么想法吗?”,我相信这里的大多数受访者都会回答“有”。但如果你对这个节目的某个方面有实际问题,请提问。在我看来,“单词”是一个单独的字符,所以每个元素都是;因为inputfile是一个字符串。我的想法是,代码可能没有达到预期的效果,但我们不知道那是什么。哦,是的,你是对的。这套守则确实有误导性。我会稍微修改一下答案。
outputfile.write(aList[-1])