我已经在这方面工作了几天,我无法让它发挥作用。我正在尝试读取文件并将一些信息累积到三个不同的字典中。这是归档在线课程的作业,但未发布解决方案。
这是要读取的文件:从下面开始。
Restaurant name to rating:
# dict of {str: int}
{'Georgie Porgie': 87%,
'Queen St. Cafe': 82%,
'Dumplings R Us': 71%,
'Mexican Grill': 85%,
'Deep Fried Everything': 52%}
Price to list of restaurant names:
# dict of {str, list of str}
{'$': ['Queen St. Cafe', 'Dumplings R Us', 'Deep Fried Everything'],
'$$': ['Mexican Grill'],
'$$$': ['Georgie Porgie'],
'$$$$': []}
Cuisine to list of restaurant names:
# dict of {str, list of str}
{'Canadian': ['Georgie Porgie'],
'Pub Food': ['Georgie Porgie', 'Deep Fried Everything'],
'Malaysian': ['Queen St. Cafe'],
'Thai': ['Queen St. Cafe'],
'Chinese': ['Dumplings R Us'],
'Mexican': ['Mexican Grill'] (--->Ends here, this part is not included).
以下是我正在使用的代码:
def read_restaurants(file):
""" (file) -> (dict, dict, dict)
Return a tuple of three dictionaries based on the information in the file:
- a dict of {restaurant name: rating%}
- a dict of {price: list of restaurant names}
- a dict of {cusine: list of restaurant names}
"""
# Initiate dictionaries
name_to_rating = {}
price_to_names = {'$': [], '$$': [], '$$$': [], '$$$$': []}
cuisine_to_names = {}
# Open the file
with open('restaurant_file_above.txt','r') as file:
# Build line list
lines = file.read().splitlines()
# Process the file
for i in range(0,len(lines),5):
# Read individual data
name = lines[i]
rating = int(lines[i+1].strip('%')) #error occurs here
price = lines[i+2]
cuisines = lines[i+3].split( ',' )
# Assign rating to name
name_to_rating[name]=rating;
# Assign names to price
price_to_names[price].append(name)
# Assign names to cuisine
for cuisine in cuisines:
cuisine_to_names.setdefault(cuisine,[]).append(name)
return name_to_rating, price_to_names, cuisine_to_names
我是初学者,所以非常感谢一些指导。我只是不知道还有什么可以尝试。顺便说一句,我正在使用Python 3.4.2。
答案 0 :(得分:0)
您尝试阅读的文件是最终输出,而不是原始文件。下面是docstring中原始数据的一个解决方案。
def read_restaurants(filename):
""" (file) -> (dict,dict,dict)
Return a tuple of three dictionaries based on the information in the file below
Georgie Porgie
87%
$$$
Canadian, Pub Food
Queen St. Cafe
82%
$
Malaysian, Thai
Dumplings R Us
71%
$
Chinese
Mexican Grill
85%
$$
Mexian
Deep Fried Everything
52%
$
Pub Food
- a dict of {restaurant name: rating}
- a dict of {price: list of restaurant names}
- a dict of {cusine: list of restaurant names}
"""
name_to_rating = {}
price_to_names = {'$':[],'$$':[],'$$$':[],'$$$$':[]}
cuisine_to_names = {}
#set incrementing number
i = 1
with open(filename,'rU') as f:
for line in f:
#check if blank line
if line.strip():
line = line.strip('\n')
#determine restaurant_name
if i == 1:
restaurant_name = line
#create dictionaries with restaurant_name
if i == 2:
rating = line
name_to_rating[restaurant_name] = rating
elif i == 3:
price = line
price_to_names[price].append(restaurant_name)
elif i == 4:
#could be multiple cuisines for each restaurant_name
cuisine_list = line.split(',')
for cuisine in cuisine_list:
cuisine_to_names[cuisine] = restaurant_name
#at blank line start increment over
else:
i = 0
i += 1
print name_to_rating
print price_to_names
print cuisine_to_names
return (name_to_rating,price_to_names,cuisine_to_names)
def main():
return read_restaurants('restaurants.txt')
if __name__ == "__main__":
main()
答案 1 :(得分:0)
我认为以上答案无法正确保存Cuisine_by_names词典。参见下面的代码。这有效:
def read_restuarants(file):
name_to_rating={}
price_to_names={'$':[],'$$':[],'$$$':[], '$$$$':[]}
cuisine_to_names={}
i=1
with open(file) as f:
contents = f.readlines()
for line in contents:
if line.strip():
l = line.strip()
if i ==1:
rest_name = l
if i ==2:
rating=l
name_to_rating[rest_name]=rating
if i==3:
price=l
price_to_names[l].append(rest_name)
if i==4:
cuisine_list = l.split(',')
for cuisine in cuisine_list:
if cuisine not in cuisine_to_names.keys():
cuisine_to_names[cuisine] = [rest_name]
else:
cuisine_to_names[cuisine].append(rest_name)
else:
i=0
i=i+1
print name_to_rating
print price_to_names
print cuisine_to_names
答案 2 :(得分:0)
这就是我解决我问题的方式。
我将在这里分享它,因为我没有在线找到任何解决方案。
我将整个文件分解为单词,并将它们保存在'lines'变量中,并考虑了以下因素使用了while循环:
lines[i] = restaurant name
lines[i+1] = rating
lines[i+2] = price
lines[i+3] = cuisine
lines[i+4] = blank
所以我使用了上面的数据,在while循环结束时,我只是将'i'加5。
这就是整个内容,包括read_restaurant:
# The file containing the restaurant data.
FILENAME = 'restaurants_small.txt'
def recommend(file, price, cuisines_list):
"""(file open for reading, str, list of str) -> list of [int, str] list
Find restaurants in file that are priced according to price and that are
tagged with any of the items in cuisines_list. Return a list of lists of
the form [rating%, restaurant name], sorted by rating%.
"""
# Read the file and build the data structures.
# - a dict of {restaurant name: rating%}
# - a dict of {price: list of restaurant names}
# - a dict of {cusine: list of restaurant names}
name_to_rating, price_to_names, cuisine_to_names = read_restaurants(file)
# Retrieves all restaurants matching specific price tag (e.g. '$') from 'price' argument
names_matching_price = price_to_names[price]
# Get restaurant names (from above price tag) that also belong to cuisine in cuisines_list
names_final = filter_by_cuisine(names_matching_price, cuisine_to_names, cuisines_list)
# Retrieve ratings from restaurants in names_final and return sorted output by ratings
result = build_rating_list(name_to_rating, names_final)
return result
def build_rating_list(name_to_rating, names_final):
""" (dict of {str: int}, list of str) -> list of list of [int, str]
Return a list of [rating%, restaurant name], sorted by rating%
>>> name_to_rating = {'Georgie Porgie': 87,
'Queen St. Cafe': 82,
'Dumplings R Us': 71,
'Mexican Grill': 85,
'Deep Fried Everything': 52}
>>> names = ['Queen St. Cafe', 'Dumplings R Us']
[[82, 'Queen St. Cafe'], [71, 'Dumplings R Us']]
"""
# For each restaurant in names_final, append corresponding rating (sorted) first to result
result = list()
for restaurant in names_final:
result.append([name_to_rating[restaurant],restaurant])
return sorted(result,reverse=True)
def filter_by_cuisine(names_matching_price, cuisine_to_names, cuisines_list):
""" (list of str, dict of {str: list of str}, list of str) -> list of str
>>> names = ['Queen St. Cafe', 'Dumplings R Us', 'Deep Fried Everything']
>>> cuis = 'Canadian': ['Georgie Porgie'],
'Pub Food': ['Georgie Porgie', 'Deep Fried Everything'],
'Malaysian': ['Queen St. Cafe'],
'Thai': ['Queen St. Cafe'],
'Chinese': ['Dumplings R Us'],
'Mexican': ['Mexican Grill']}
>>> cuisines = ['Chinese', 'Thai']
>>> filter_by_cuisine(names, cuis, cuisines)
['Queen St. Cafe', 'Dumplings R Us']
"""
# For each cuisine we want, check if the names within price range belong to that cuisine
result = list()
# For each name that is in cuisines_list, we check if restaurant name is in there and return it
for cuisine in cuisines_list:
for name in names_matching_price:
if name in cuisine_to_names[cuisine] and name not in result:
result.append(name)
return result
def read_restaurants(file):
""" (file) -> (dict, dict, dict)
Return a tuple of three dictionaries based on the information in the file:
- a dict of {restaurant name: rating%}
- a dict of {price: list of restaurant names}
- a dict of {cusine: list of restaurant names}
"""
name_to_rating = {}
price_to_names = {'$': [], '$$': [], '$$$': [], '$$$$': []}
cuisine_to_names = {}
with open(file) as f:
i = 0
# Store all lines (without \n) to lines in a list of words
lines = f.read().splitlines()
while i < len(lines):
# we know the following:
# line[i] = restaurant name, line[i+1] = rating
# line[i+2] = price, line[i+3] = cuisine
name_to_rating[lines[i]] = lines[i+1]
# match price to name
if lines[i+2] not in price_to_names.keys():
price_to_names[lines[i+2]] = [lines[i]]
else:
price_to_names[lines[i+2]].append(lines[i])
# add list of restaurants per cuisine
cuisines = lines[i+3].split(',')
for cuisine in cuisines:
if cuisine not in cuisine_to_names.keys():
cuisine_to_names[cuisine] = [lines[i]]
else:
cuisine_to_names[cuisine].append(lines[i])
# while loop is incremented by 5 as name is in position 0, rating 1, price 2, cuisine 3, blank line 4
i += 5
return name_to_rating, price_to_names, cuisine_to_names