python - 通过python计算文本文件中的单词

标签 python string parsing python-3.x

下面是我分配的一个项目,我必须在其中编写一个程序来打开一个文本文件并计算文件中的单词数。下面我概述了一个程序,该程序应该为任何空白情况(即空格、制表符、\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/

相关文章:

c# - 程序未运行/进入调试点

c# - 如何更改字符串或 double 的文化信息

python - 如何在centos中安装python3-tk?

python - fortran中的btest函数,python中有等效的函数吗?

python - 格式 () : ValueError: Precision not allowed in integer format specifier

java - 如何将文件与 String java 中的文件完全一样保存

java - 在 Java 代码中使用 Grep

php - MPRandomText 类的对象无法转换为字符串

python - 如何在 Visual Studio 终端中自动激活 python 虚拟环境?

regex - 使用 grep 解析文本