我有一个文件(census.txt),我正在尝试将其提取为嵌套字典。该文件已被复制到代码中以便于编程。 我的问题是,我无法以某种方式迭代和“更新”字典。 你能给点灯吗?
如果有人可以提供一些更新字典的提示,我想进一步尝试。 这是我所拥有的:
#!/usr/bin/python
#
#
####### census.txt #################
#day0 nameOfCity count records
#day0 city0 1 2
#day0 city1 5 6
#day0 city2 6 12
#day1 nameOfCity count records
#day1 city0 1 2
#day1 city1 7 5
#day1 city2 6 12
#day2 nameOfCity count records
#day2 city0 1 2
#day2 city1 7 5
#day2 city2 6 12
#day2 city0 4 3
#######################################
#
#Note: # sum values of data for same city entered multiple times
#
##### REQUIRED NESTED DICTIONARY OUTPUT ###############
#
#{ day0: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 5, 'records' : 6}, city2: {'count' : 6, 'records' : 12}},
# day1: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}},
# day2: {city0 : {'count': 5, 'records' : 5}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}} }
#
# #######################################################
import sys
import os
# ===================
# Main Python section
# ===================
if __name__ == '__main__':
omit_list =['nameOfCity'] # omit the heading line each time and pick only values
count_rows = 0
count_columns = 4 #known beforehand,
items = [0 for i in range (count_columns)]
dict2 = {} #outermost dictionary with day as key
dict1 = {} #inner dictionary with city names as keys
dict0 = {} #innermost dictionary with count and records as keys
with open('census.txt', 'r') as fname:
for line in fname:
if (not any(omitted_word in line for omitted_word in omit_list)) and line.strip():
items =line.split()
if len(items) == count_columns:
dict0["count"]=items[2]
dict0["records"]=items[3]
dict1[items[1]]= dict0
if items[0] not in dict2:
dict2[items[0]] = dict1
print 'if'
else:
dict2[items[0]].update(dict1)
print 'else'
dict0 = {}
print dict2
count_rows +=1
#print count_rows
print "*********** dict2 ************"
print dict2
fname.close()
我正在尝试提取每个城市的每日信息。在这里,编码时的第一个问题是,例如,第2天的city0已被多次输入。
由于关键字段“ city0”是唯一的,因此在“创建”最里面的记录和计数字典之前,我需要总结记录和计数的数量。另一件事是,“ day”键是唯一的,但包含许多城市字段值。我不知何故无法实现此嵌套和代码的输出:
*********** dict2 ************
{'day2': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}, 'day0': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}, 'day1': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}}
(这显然是错误的,因为仅最里面的字典仅被day2信息覆盖)
我期望的实际输出是:
{ day0: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 5, 'records' : 6}, city2: {'count' : 6, 'records' : 12}},
day1: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}},
day2: {city0 : {'count': 5, 'records' : 5}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}} }
答案 0 :(得分:1)
如果文件一致,则应遵循以下步骤:
days = {}
current_day = None
with open('census.txt') as fname:
for l in fname:
day, city, count, records = l.split()
if current_day != day:
current_day = day
days[current_day] = {}
else:
count = int(count)
records = int(records)
if city in days[current_day]:
days[current_day][city]['count'] += count
days[current_day][city]['records'] += records
else:
days[current_day][city] = {'count': count, 'records': records}
print(days)
还请注意:如果使用.close()
语句,则无需with
文件。它会自动关闭。
答案 1 :(得分:0)
感谢上述文章中的代码。这对我有用:
GET