Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating more than one data structure (dicts) in Python

Novice programmer here. Self-Learning Python. First question on Stackoverflow.

I am trying to write a program to recommend a restaurant based on user's selection of price, rating and cuisine type. To achieve this, the program builds three data structures: [I am still at an intermediate stage]

# Initiating the data structures

name_rating = {}
price_name = {}
cuisine_name = {}

The data comes from restaurants.txt with the following formatting:

#Rest name
#Rest Rating
#Rest Price range
#Rest Cuisine type
#
#Rest2 name

etc

The following function returns only the string at the desired line

# The get_line function returns the 'line' at pos (starting at 0)
def get_line(pos):
    fname = 'restaurants.txt'
    fhand = open(fname)
    for x, line in enumerate(fhand):
        line = line.rstrip()
        if not pos == x: continue
        return line


# Locating the pos's of name, rate, price & cuisine type for each restaurant
# Assumes uniform txt file formatting and unchanged data set 

name_pos = [x for x in range(0,84,5)]
rate_pos = [x for x in range(1,84,5)]
price_pos = [x for x in range(2,84,5)]
cuis_pos = [x for x in range(3,84,5)]

Increments by 5 to get data for each restaurant separately.

fname = 'restaurants.txt'
fhand = open(fname)

The following returns a dictionary of name: ratings

# Builds the name_rating data structure (dict)
def namerate():
    for x, line in enumerate(fhand):
        line = line.rstrip()
        for n, r in zip(name_pos, rate_pos):
            if not n == x: continue
            name_rating[line] = name_rating.get(line, get_line(r))
    return name_rating 

The following returns a dictionary of price: name

# Builds the price_name data structure (dict)
def pricename():
    for x, line in enumerate(fhand):
        line = line.rstrip()
        for p, n in zip(price_pos, name_pos):
            if not p == x: continue
            price_name[line] = price_name.get(line, get_line(n))
    return price_name

Calling the functions

print pricename()
print namerate()

QUESTION: When I call the functions, why is it that only the one that I call first is successful? The second dict remains empty. If i call them separately, the data structures are built. If i call both, only the first is successful.

p.s. I'm sure I could do all this a lot faster but for now I'm trying to do it myself so some may seem redundant or unnecessary. Please bear with me :)

like image 214
Abd00s Avatar asked Nov 10 '22 06:11

Abd00s


1 Answers

You can set the file position to the start again using seek method (see docs ):

f = open("test.txt")

for i in f:
    print(i)
# Print: 
    # This is a test first line
    # This is a test second line

for i in f:
    print(i)
# Prints nothig   

f.seek(0) # set position to the beginning

for i in f:
    print(i)
# Print: 
    # This is a test first line
    # This is a test second line
like image 143
Ruben Bermudez Avatar answered Nov 14 '22 22:11

Ruben Bermudez