从文本文件填充词典

时间:2013-03-26 04:53:17

标签: python file io dictionary

我有一个文本文件,其中包含一组接一个给出的餐馆的详细信息。详细信息包括特定餐厅的名称,评级,价格和菜肴类型。文本文件的内容如下所示。

George Porgie
87%
$$$
Canadian, Pub Food

Queen St. Cafe
82%
$
Malaysian, Thai

Dumpling R Us
71%
$
Chinese

Mexican Grill
85%
$$
Mexican

Deep Fried Everything
52%
$
Pub Food

我想创建一组字典,如下所示:

Restaurant name to rating:
# dict of {str : int}
name_to_rating = {'George Porgie' : 87,
'Queen St. Cafe' : 82,
'Dumpling R Us' : 71,
'Mexican Grill' : 85,
'Deep Fried Everything' : 52}

Price to list of restaurant names:
# dict of {str : list of str }
price_to_names = {'$'   :  ['Queen St. Cafe', 'Dumpling R Us', 'Deep Fried Everything'],
'$$'  :  ['Mexican Grill'],
'$$$' :  ['George Porgie'], 
'$$$$' : [ ]}

Cuisine to list of restaurant name:
#dic of {str : list of str }
cuisine_to_names = {'Canadian' : ['George Porgie'],
'Pub Food' : ['George Porgie', 'Deep Fried Everything'],
'Malaysian' : ['Queen St. Cafe'],
'Thai' : ['Queen St. Cafe'],
'Chinese' : ['Dumpling R Us'],
'Mexican' : ['Mexican Grill']}

Python填充上述词典的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

初始化一些容器:

name_to_rating = {}
price_to_names = collections.defaultdict(list)
cuisine_to_names = collections.defaultdict(list)

将您的文件读入临时字符串:

with open('/path/to/your/file.txt') as f:
  spam = f.read().strip()

假设结构是一致的(即由两条换行符分隔的4行的块),遍历块并填充容器:

restraunts = [chunk.split('\n') for chunk in spam.split('\n\n')]
for name, rating, price, cuisines in restraunts:
  name_to_rating[name] = rating
  # etc ..

答案 1 :(得分:0)

对于主读取循环,您可以使用枚举和模数来知道一行上的数据:

for lineNb, line in enumerate(data.splitlines()):
    print lineNb, lineNb%4, line

对于price_to_namescuisine_to_names字典,您可以使用defaultdict:

from collections import defaultdict
price_to_names = defaultdict(list)