python删除文本中指定内容并统计每个字出现字数_Python:如何从一行特定的文本中获取字数,并保存它以在I上进行计算...

我正在计算文件中的字数,然后我想计算这些数字。现在,这个代码只保存最后一行代码。我怎样才能让它工作?你知道吗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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值