我需要这种格式,但我想我不知道有关字典的一些更重要。我有文本文件,我想以下面给出的格式转换文本文件:
gameConfiguration.txt
#Rooms
room Hall
room Kitchen
room Storage
room Bedroom
room Bathroom
room Garage
#Doors
door N open Hall Bedroom
door N closed Storage Kitchen
door E locked Hall Storage
door E open Bedroom Kitchen
door N open Bedroom Bathroom
door S open Bedroom Hall
door S open Bathroom Bedroom
door W locked Hall Garage
door W open Kitchen Bedroom
door S closed Kitchen Storage
door W open Storage Hall
door E locked Garage Hall
#Items
item car Garage STATIONARY
item remote Kitchen MOVE
item key Bedroom USE
item key Hall USE
#Start position
start Hall
字典
rooms={"Hall":{"name":"Hall","N":{"dir":"Bedroom","status":"open"},"E":{"dir":"Storage","status":"locked"},"W":{"dir":"Garage","status":"locked"},"directions":["N","W","E"],"item":"key"},
"Bedroom":{"name":"Bedroom","E":{"dir":"Kitchen","status":"open"},"N":{"dir":"Bathroom","status":"open"},"S":{"dir":"Hall","status":"open"},"directions":["E","N","S"],"item":"key","start":1},
"Storage":{"name":"Storage","N":{"dir":"Kitchen","status":"closed"},"W":{"dir":"Hall","status":"open"},"directions":["N","W"],"start":"Hall"},
"Garage":{"name":"Garage","E":{"dir":"Hall","status":"locked"},"directions":["E"],"item":"car","start":"Hall"},
"Kitchen":{"name":"Kitchen","W":{"dir":"Bedroom","status":"open"},"S":{"dir":"Storage","status":"closed"},"directions":["W","S"],"item":"remote","start":"Hall"},
"Bathroom":{"name":"Bathroom","S":{"dir":"Bedroom","status":"open"},"directions":["S"],"start":"Hall"}}
我忽略那些以#开头的行并将这些行拆分成单词并尝试将字符串转换为字母。
我的代码:
d={}
def dictnPart():
with open("gameconfiguration.txt",'r') as input_data:
for line in input_data:
li=line.strip()
if not li.startswith("#"):
abc=line.strip().split()
# print(abc[1])
if abc[0]=="room":
d={abc[1]:{"name":abc[1]}}
# print(d)
if abc[0]=="door":
d.update({abc[1]:{"dir":abc[4],"status":abc[2]},"directions":[abc[1]]})
# print(d)
if abc[0]=="item":
d.update({"item":abc[1],"directions":abc[2]})
print(d)
dictnPart()
但主要的问题是它与最近的一个重叠并且我得到了什么:
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Garage', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'car'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Kitchen', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'remote'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Bedroom', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'key'}
{'Garage': {'name': 'Garage'}, 'N': {'dir': 'Bathroom', 'status': 'open'}, 'directions': 'Hall', 'E': {'dir': 'Hall', 'status': 'locked'}, 'S': {'dir': 'Storage', 'status': 'closed'}, 'W': {'dir': 'Hall', 'status': 'open'}, 'item': 'key'}
帮助我获得理想的输出。 请!!
答案 0 :(得分:0)
door
和item
的行上都有房间名称。用它来访问字典的正确元素。
d={}
def dictnPart():
with open("gameconfiguration.txt",'r') as input_data:
for line in input_data:
li=line.strip()
if not li.startswith("#"):
abc=line.strip().split()
# print(abc[1])
if abc[0]=="room":
d[abc[1]] = {"name":abc[1]}
# print(d)
if abc[0]=="door":
roomname = abc[3]
d[roomname].update({abc[1]:{"dir":abc[4],"status":abc[2]}})
if "directions" in d[roomname]:
d[roomname]["directions"].append(abc[1])
else:
d[roomname]["directions"] = [abc[1]]
# print(d)
if abc[0]=="item":
roomname = abc[2]
d[roomname].update({"item":abc[1]})
if "directions" in d[roomname]:
d[roomname]["directions"].append(abc[2])
else:
d[roomname]["directions"] = [abc[2]]
print(d)