下面是我分配的一个项目,我必须在其中编写一个程序来打开一个文本文件并计算文件中的单词数。下面我概述了一个程序,该程序应该为任何空白情况(即空格、制表符、\n)设置 previous = False
并设置 previous = True
任何其他情况。当存在 previous = False
且未检测到空格(单词开头)的情况时,它将向 wordCount
加 1。但是,我的输出结果略有不同(如下所示)。限制是我不能使用 .split() 函数,必须手动完成。由于这是一项学校作业,我并不是要找人帮我做这件事,而只是教我一些知识并解释我做错了什么。
代码:
"""
title: findWords.py
author: Riley Lloyd
"""
#import necessary libraries
import turtle as bob
def init():
"""
:return:
"""
pass
def countWords ( textFileName ):
"""
:param textFileName:
:return:
"""
previous = False
wordCount = 0
for text in open(textFileName):
print(text)
if text == " ":
previous = False
elif text == "\n":
previous = False
elif text == "\t":
previous = False
else:
if previous == False:
wordCount += 1
previous = True
print(wordCount)
def main():
"""
:return:
"""
init()
countWords(input("Enter filename: "))
main()
结果:
Enter filename: some.txt
Words make up other words.
This is a line.
Sequences of words make sentences.
I like words but I don't like MS Word.
There's another word for how I feel about MSWord: @#%&
1
Process finished with exit code 0
最佳答案
您正在遍历打开的文件 -
for text in open(textFileName):
当你这样做时,你实际上是在遍历文件的行,所以在第一次迭代中 text
将是文件的第一行,在第二次迭代中 text
将是文件的第二行,等等。但是,您的逻辑是这样编写的,它期望 text
将是文件中的每个字符。
如果您的文件不大,我建议您执行 .read()
并对其进行迭代。示例 -
def countWords ( textFileName ):
"""
:param textFileName:
:return:
"""
with open(textFileName) as f:
texts = f.read()
previous = False
wordCount = 0
for text in texts:
print(text)
if text == " ":
previous = False
elif text == "\n":
previous = False
elif text == "\t":
previous = False
else:
if previous == False:
wordCount += 1
previous = True
print(wordCount)
我已经使用with
语句打开文件,你也应该使用with
语句打开文件,它会自动为你处理关闭文件。
关于python - 通过python计算文本文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32620991/