Isaiah
Isaiah

Reputation: 3

find a string in a file and print line for multiple lines

So to start off I have a folder named data with .txt files in the folder that are the acronyms for each US state. These files have the top few most popular names for that year and state. A couple lines from a random file look like so

AK,F,1910,Mary,14 

AK,F,1910,Annie,12 

AK,F,1910,Anna,10

AK,F,1910,Margaret,8

AK,F,1910,Helen,7

AK,F,1910,Elsie,6

AK,F,1910,Lucy,6

AK,F,1910,Dorothy,5

AK,F,1911,Mary,12

AK,F,1911,Margaret,7

AK,F,1911,Ruth,7

AK,F,1911,Annie,6

AK,F,1911,Elizabeth,6

AK,F,1911,Helen,6

My task is to find the most popular name for a range of years given by user (the gender is also given by user. As you can see the most popular name is the first one with the new year on it. My code is as follows:

def getTopNames(state, gender, startYear, endYear):
    names = []
    file = open("data/" + state + ".TXT")
    with open(file) as f:
        for line in f:
            if startYear and gender in line:
                names.append(line.split(","))
                if startYear < endYear:
                    startYear += 1
                    names.append(line.split(","))
    print ( lstAll )

This is the error I get:

File "C:/Python34/CS1 WORK/top_names.py", line 53, in getTopNames
   with open(file) as f:
 TypeError: invalid file: <_io.TextIOWrapper name='data/NY.TXT' mode='r' encoding='cp1252'>

what am I doing wrong? I made sure the data file is in the same file that my .py file is in.

Upvotes: 0

Views: 124

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

You're trying to open the file twice:

file = open("data/" + state + ".TXT")
with open(file) as f:

and should be using

filename = "data/" + state + ".TXT"
with open(filename) as f:

Then, the line

if startYear and gender in line:

doesn't work as you think it does. It actually means

if (startYear) \ # true if startYear is non-zero/nonempty
and (gender in line): # true if the letter M/F occurs *anywhere* in the line

You want something like

items = line.split(",")
if str(startYear) in items and gender in items:

Upvotes: 2

Related Questions