2014-05-05 46 views
-1
import sys 
import pickle 
import string 

def Menu(): 
    print ("***********MENU************") 
    print ("0. Quit") 
    print ("1. Read text file") 
    print ("2. Display counts") 
    print ("3. Display statistics of word lengths") 
    print ("4. Print statistics to file") 

def readFile(): 
    while True: 
      fileName = input("Please enter a file name: ") 
     if (fileName.lower().endswith(".txt")): 
       break 
     else: 
       print("That was an incorrect file name. Please try again.") 
     continue 
    return fileName 


THE_FILE = "" 
myDictionary = 0 

def showCounts(fileName):  
    numCount = 0 
    dotCount = 0 
    commaCount = 0 
    lineCount = 0 
    wordCount = 0 

    with open(fileName, 'r') as f: 
      for line in f: 
       wordCount+=len(line.split()) 
       lineCount+=1 
       for char in line: 
         if char.isdigit() == True: 
          numCount+=1 
         elif char == '.': 
          dotCount+=1 
         elif char == ',': 
          commaCount+=1 

    print("Number count: " + str(numCount)) 
    print("Comma count: " + str(commaCount)) 
    print("Dot count: " + str(dotCount)) 
    print("Line count: " + str(lineCount)) 
    print("Word count: " + str(wordCount)) 

def showStats(fileName): 
    temp1 = [] 
    temp2 = [] 
    lengths = [] 
    myWords = [] 
    keys = [] 
    values = [] 
    count = 0 

    with open(fileName, 'r') as f: 
     for line in f: 
      words = line.split() 
      for word in words: 
       temp2.append(word) 
       temp1.append(len(word)) 

     for x in temp1: 
      if x not in lengths: 
       lengths.append(x) 

     lengths.sort() 

    dictionaryStats = {} 
    for x in lengths: 
     dictionaryStats[x] = [] 

    for x in lengths: 
     for word in temp2: 
      if len(word) == x: 
       dictionaryStats[x].append(word) 

    for key in dictionaryStats: 
     print("Key = " + str(key) + " Total number of words with " + str(key) + " characters = " + str(len(dictionaryStats[key])))   
    return dictionaryStats 

def printStats(aDictionary): 
    aFile = open("statsWords.dat", 'w') 
    for key in aDictionary: 
     aFile.write(str(key) + " : " + str(aDictionary[key]) + "\n")   
    aFile.close() 

choice = -1 

while choice !=0: 
    Menu() 
    choice = (int(input("Please choose 1-4 to perform function. Press 0 to exit the program. Thank you. \n"))) 
    if choice == 0: 
     print ("Exit program. Thank you.") 
     sys.exit 
    elif choice == 1: 
      THE_FILE = readFile() 
    elif choice == 2: 
      showCounts(THE_FILE) 
    elif choice == 3: 
      showStats(THE_FILE) 
    elif choice == 4: 
      printStats(myDictionary)   
    else: 
     print ("Error.") 

我试图打开一个文件,使其显示的字长的统计数据,然后把它做一个新的文件与字长的统计信息。我可以读取文件并显示统计信息,但是当我将统计信息打印到文件时,出现错误 - “int”对象不可迭代。有任何想法吗?多谢你们!Python的 - 从一个文件打印统计到另一个

Error: 

Traceback (most recent call last): 
    File "hw4_ThomasConnor.py", line 111, in <module> 
    printStats(myDictionary)   
    File "hw4_ThomasConnor.py", line 92, in printStats 
    for key in aDictionary: 
TypeError: 'int' object is not iterable 
+0

请包括实际的错误/追溯,它应该包括错误的行号 – Totem

+0

确实只是抱歉 – TommyConnor

+0

提供完整的堆栈跟踪怎么样 - 它可能会指向您的代码中的确切行,并会告诉你,你将迭代整数。 –

回答

1

的问题是,你在你的程序的顶部设置myDictionary0,然后在这里printStats(myDictionary)发送给你的文件写入功能。

在这个函数中,你有这条线for key in aDictionary,并且由于你通过0,这实际上是for key in 0这是错误来自的地方。

您需要将showStats函数的结果发送到您的printStats函数。

由于这看起来像家庭作业,我现在就离开它。


对不起,我很困惑。在showStats函数中,我必须以某种方式说 “发送结果到printStats函数”,然后在printStats 函数中我必须调用结果?我将如何做到这一点?

printStats函数需要打印字典。这个字典是由showStats函数生成的(实际上,它返回这个字典)。

所以你需要将showStats函数的结果发送给printStats函数。

要保存方法的返回值,你可以指定它的调用表达式的LHS(左侧),像这样:

>>> def foo(bar): 
... return bar*2 
... 
>>> def print_results(result): 
... print('The result was: {}'.format(result)) 
... 
>>> result = foo(2) # Save the returned value 

由于result就像在Python任何其他名称,你可以将它传递给任何其他功能:

>>> print_results(result) 
The result was: 4 

如果我们不想存储功能的结果,只是想将它发送到另一个功能,那么我们就可以用这个语法:

>>> print_results(foo(2)) 
The result was: 4 

您需要在执行功能的主循环中执行类似操作。

既然你要打印的字典由showStats函数返回,必须调用showStats第一函数调用的函数printStats之前。如果您的用户在选择3之前选择了4,这会产生问题 - 请确保您找到解决方法。一个简单的解决办法将是提示用户通过选择4.再想想另一种方式来解决这个问题之前选择(3)计算统计。

+0

对不起,我很困惑。在showStats函数中,我必须以某种方式说“将结果发送到printStats函数”,然后在printStats函数中我必须调用结果?我将如何做到这一点? – TommyConnor

0

这里:

​​3210

你设置为myDictionary整数。

,以后你做:

printStats(myDictionary) 

,当你尝试字典的键interate在里面,你失败了。

相关问题