我想要計算文件中單詞的數量,然後我想對這些數字進行一些計算。現在,這段代碼只會保存最後一行代碼。我怎樣才能使這個工作?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)
你爲什麼計算三次相同的「wordCount(發送)」? 'readFile'只返回最後一行,因爲這是你保存的唯一一行。你通過以前的,但不要對他們做任何事情。 – TigerhawkT3
@TimCastelijns - 我想你的意思是'len(sentence.strip()。split())'或者'sentence.count('')'(如果一個空格是保證的空格,我希望它是,給出OP的算法)。 – TigerhawkT3
@ TigerharkT3 - 我將如何保存以前的? –