Python:从带有输入的txt文件计算Word的出现次数

时间:2016-11-30 20:38:32

标签: python input

我是编程新手,我需要帮助查找用户输入在txt文件中发生的次数。我目前的代码是:

myfile = open("WorldSeriesWinners.txt")

count = 0
team = input("Enter in the team name that won the world series: ")
line = myfile.readline()

myfile.readline()

while team in line:
    count += 1

myfile.readline()

print("The", team, "won the world series", count, "times")



myfile.close()

我得到的输出是:

Enter in the team name that won the world series: New York Yankees
The New York Yankees won the world series 0 times

如何让特定球队获胜多次?提前致谢。

3 个答案:

答案 0 :(得分:0)

team = input('Enter team name: ')
count = 0
with open("WorldSeriesWinners.txt") as f:
    for line in f:
        if team in line:
            count += 1

print('{} won the world series {} times'.format(team, count)

逐行浏览并使用if语句检查每一行

答案 1 :(得分:0)

尝试以下方法:

import re

def world_series_count(team):
    with open("WorldSeriesWinners.txt") as f:
        data = f.read()
        items = re.findall(team, data)
    return len(items)

team = input('Enter a team name: ')

count = world_series_count(team)

print('{} won the world series {} times'.format(team, count))

答案 2 :(得分:0)

为什么你的人在必要时使事情复杂化。

这将计算路径给出的txt文件中文本的出现次数,而不管文本格式(除非它是连字符):

def count_in_file (path, text):
    f = open(path, "rb")
    c = f.read()
    f.close()
    return " ".join(c.split()).count(text.strip())

需要稍微调整才能支持unicode txt文件。但那里。简单易行。

如果txt文件非常大,那么使用静态块大小缓冲执行此操作:

def count_in_file (path, text, chunksize=4096):
    text = text.strip()
    f = open(path, "rb")
    f.seek(0, 2)
    fsize = f.tell()
    f.seek(0)
    if fsize<=chunksize:
        c = f.read()
        f.close()
        return " ".join(c.split()).count(text)
    count = 0
    l = len(text)
    c = f.read(chunksize)
    r = c[-l:]
    while c:
        count += " ".join(c.split()).count(text)
        if r!=text: f.seek(f.tell()-l+1)
        c = f.read(chunksize)
        r = c[-l:]
    f.close()
    return count

嗯,现在这有点复杂。但是如果一个文件真的非常大,并且它是按行格式化的,那么这是一个很好的方法。