2

I am new to programming and I need help finding how many times the user input occurs inside of a txt file. The code I currently have is:

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

The output I get from this is:

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

How would I get it to show many times a specific team won? Thanks in advance.

MBayuk
  • 27
  • 2

3 Answers3

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)

Go through line by line and check each line using if statements

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • This is a good solution if we assume the file has the whole team name on one line (like a list) and is not broken over a new line (like in a paragraph with some formatting restrictions). – sudobangbang Nov 30 '16 at 20:50
  • Thank you for the help! I have a better understanding of this now. – MBayuk Nov 30 '16 at 20:57
  • 1
    @sudobangbang that's not something i'd expect to run into on text files, but in that case you could probably read in the file and remove all the `\n` characters or something. Then you run into the problem of matching lines that end in `New York` and the next line says `Yankees`, but it was not the authors intent to record the team. – Patrick Haugh Nov 30 '16 at 20:57
  • @PatrickHaugh, agreed, I still second your original request for an example of the file to read – sudobangbang Nov 30 '16 at 21:04
0

Try the following:

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))
ettanany
  • 19,038
  • 9
  • 47
  • 63
0

Why are you people complicating matters way over necessary.

This will count occurrences of text in a txt file given by path, regardless of text formatting (unless it is hyphenized):

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

A little tweak will be needed to support unicode txt files. But there. Simple and easy.

If the txt file is extremely big, then perform this using buffering with static chunk size:

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

Well, this now is a bit complicated. But if a file is really, really big, and it is formatted over lines, this is a good way to do it.

Dalen
  • 4,128
  • 1
  • 17
  • 35