2015-10-21 64 views
0

我想要計算文件中單詞的數量,然後我想對這些數字進行一些計算。現在,這段代碼只會保存最後一行代碼。我怎樣才能使這個工作?Python:我如何從特定的文本行中獲取字數並將其保存爲對其進行計算

def fileVerify(): 
    start = 0 
    while start == 0: 
     fileName = input("Please enter the name of the file you want to open ") 
     try: 
      inFile = open(fileName, "r") 
      inFile.close() 
      start = 1 
     except Exception as ex: 
      print("Could not open ", fileName) 
      print(" Actual exception error message is:") 
      print(" "+str(ex)) 
     return fileName 


def readFile(fileName): 
    inFile = open(fileName, "r") 
    count = 1 
    for lineOfText in inFile: 
     print(count,": ",lineOfText,end="") 
     count = count + 1 
     print(" >>", wordCount(lineOfText), "words") 
    return lineOfText 


def wordCount(sentence): 
    wordCount=0 
    sentence=sentence.strip() 
    for i in range (0, len(sentence)): 
     if (sentence[i]==" "): 
      wordCount=wordCount+1 
    if (len(sentence)>0): 
     wordCount=wordCount+1 
     return wordCount 


def wordAverage(a,b): 
    average = a/b 
    print("The average words per lines are",average) 


def minWords(x,y,z): 
    if x<=y and x<=z: 
     print("Least words in a line:", x) 
    elif y<=x and y<=z: 
     print("Least words in a line:", y) 
    else: 
     print("Least words in a line:", z) 


def maxWords(x,y,z): 
    if x>=y and x>=z: 
     print("Most words in a line:", x) 
     return x 
    elif y>=x and y>=z: 
     print("Most words in a line:", y) 
     return y 
    else: 
     print("Most words in a line:", z) 
     return z 


def totalWords(x,y,z): 
    total=x+y+z 
    print("Total words in input: ", total) 


def totalLines(fileName): 
    inFile = open(fileName, "r") 
    count = 1 
    for lineOfText in inFile: 
     count = count + 1 
    inFile.close() 
    return count 

這是我的主要內容。我試圖讓x成爲第一個輸入,y成爲下一個,而z成爲我的最後一個。

#main 

print("Welcome to file analysis") 

fileName=fileVerify() 
sent=readFile(fileName) 

x=wordCount(sent) 
y=wordCount(sent) 
z=wordCount(sent) 

print("\nAnalysis") 
print("===========") 

minWords(x,y,z) 
a=maxWords(x,y,z) 
b=totalLines(fileName) 
wordAverage(a, b) 
totalWords(x,y,z) 
+0

你爲什麼計算三次相同的「wordCount(發送)」? 'readFile'只返回最後一行,因爲這是你保存的唯一一行。你通過以前的,但不要對他們做任何事情。 – TigerhawkT3

+0

@TimCastelijns - 我想你的意思是'len(sentence.strip()。split())'或者'sentence.count('')'(如果一個空格是保證的空格,我希望它是,給出OP的算法)。 – TigerhawkT3

+0

@ TigerharkT3 - 我將如何保存以前的? –

回答

1

有一個可能更容易計算單詞的方法。而不是你的句子計數的空格數,您可以創建基於把句子,像這樣的列表:

>>> sentence = "The quick brown fox jumped over the lazy dog." 
    >>> splitSentence = sentence.split() 
    >>> splitSentence 
    ['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog.'] 
    >>> len(splitSentence) 
    9 

.split()是創建由分割原始字符串列表的字符串方法(默認情況下)或者你選擇的子字符串(例如,.split(「,」)會以逗號分隔)。您可以非常容易地測試後續列表的長度。

0
words = len(sentence.split()) 

words將字符串sentence中的字的數量。

相關問題