2012-04-25 55 views
-2

这是我的。我想写一个程序到一个文件

""" 

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()** 

的txt文件是从我的教授:P

加粗的部分是最重要的,我相信。

有什么想法?

+4

您是要求一般反馈还是您有一个问题要解决?如果您需要一般反馈,[代码评论](http://codereview.stackexchange.com/)网站将更适合这类问题。 – Darthfett 2012-04-25 21:21:38

+1

您问的唯一问题是“有什么想法?”,我相信大部分受访者会回答“是”。但是,如果您对该计划的某个方面有实际的疑问,请提出。 – 2012-04-25 21:23:00

回答

2

您是否尝试过运行代码?

我认为,如果你会,你会得到关于行

outpufile.write(aList) 

看到一条错误消息,为file.write()方法的文档字符串中明确规定:

写(STR) - >无。将字符串str写入文件。

请注意,由于缓冲区的原因,在 之前可能需要使用flush()或close()方法来反映写入的数据。

您正在提供它list而不是str。尝试将其更改为

outpufile.write(''.join(aList)) 

outputfile.write(aList[-1]) 

或任何适合您的需求。此外,你永远不会清除列表,所以,当你遍历inputfile时,你会写第一个字符,然后是第一个字符,然后是第一个字符,然后是前三个字符,等等。

最后,您试图关闭inputfile这实际上是str,而不是文件,因为file.read()方法返回str

P.S.请不要致电变量inputfile如果它是一个字符串,并且words如果它是一个字符。那永远不会帮助任何人。

+1

在我看来,'单词'是单个字符,所以aList的每个元素也是;因为inputfile是一个字符串。我的想法是,代码可能不会做它打算做的事,但我们不知道那是什么。 – greggo 2012-04-25 22:07:38

+0

哦,是的,你是对的。这段代码真的有误导性。我会稍微编辑答案。 – 2012-04-25 22:09:48

相关问题