【问题标题】:Python: Counting Occurrences of a Word From a txt file with InputPython:计算带有输入的 txt 文件中单词的出现次数
【发布时间】:2016-11-30 20:38:31
【问题描述】:

我是编程新手,我需要帮助找出用户输入在 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

如何让它显示特定球队获胜的次数?提前致谢。

【问题讨论】:

  • 你能贴几行文本文件吗?
  • 如果该团队在排队,那么您自己就有一个无限循环,您可能应该在线和团队使用lower()upper(),以便案例匹配
  • 你能不能在行中使用split(),然后在结果列表中使用count()
  • @ElliotRoberts 在使用count() 之前无需split,因为它适用于字符串

标签: python input


【解决方案1】:
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语句检查每一行

【讨论】:

  • 如果我们假设文件在一行中包含整个团队名称(如列表)并且没有换行(如在具有某些格式限制的段落中),这是一个很好的解决方案。
  • 感谢您的帮助!我现在对此有了更好的理解。
  • @sudobangbang 这不是我希望在文本文件中遇到的,但在这种情况下,您可能可以读取文件并删除所有 \n 字符或其他内容。然后你会遇到匹配以New York 结尾的行并且下一行显示Yankees 的问题,但作者无意记录团队。
  • @PatrickHaugh,同意,我仍然支持您最初要求的文件示例
【解决方案2】:

尝试以下方法:

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))

【讨论】:

  • 这里的正则表达式似乎有点不必要。
  • 为什么不呢?我认为这很好,因为我们不知道名称的确切格式。
【解决方案3】:

为什么你们把事情复杂化了。

这将计算由路径给出的 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 2021-06-25
    • 2015-11-28
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多