我有一个文本文件,其内容编写如下:
State Texas
Austin
Houston
Dallas
State Florida
Orlando
Miami
Jacksonville
Naples
!
State California
San Diego
Los Angeles
San Francisco
目标:我想从此文本文件中读取并将其转换为字典,并且应该像这样-
state_dict = {
'Texas': ['Austin', 'Houston', 'Dallas'],
'Florida': ['Orlando', 'Miami', 'Jacksonville', 'Naples'],
'California': ['San Diego', 'Los Angeles', 'San Francisco']
}
到目前为止,我的代码是这样:
State_Dict = {}
with open('state.txt', 'r') as main_fd:
for mystate in main_fd:
mystate = mystate.lstrip()
if ("State" in mystate):
state_key = "_".join(mystate.split()[1:])
State_Dict[state_key] = []
for cities in main_fd:
if ("!" in cities):
break
else:
State_Dict[state_key].append(cities.rstrip())
print(State_Dict)
但是输出是这样的:
{
'Texas': [
' Austin', ' Houston', ' Dallas',
'State Florida', ' Orlando', ' Miami',
' Jacksonville', ' Naples'
],
'California': [
' San Diego', ' Los Angeles', ' San Francisco'
]
}
该如何解决?
答案 0 :(得分:2)
您正在寻找的是这样的东西。 考虑一次循环遍历一行,并针对您的情况进行一系列检查。 PS。让我知道这是否行不通,未经测试就写了。
State_Dict = {}
with open('state.txt', 'r') as main_fd:
for line in main_fd:
line = line.strip() #left and right stripped
#if ("state" in line.lower()): #better version suggested in comments to handle 'state' and 'State'.
if ("State" in line):
state_key = "_".join(line.split()[1:])
State_Dict[state_key] = []
elif ("!" in line):
continue #goes to next iteration of loop instead of stopping the loop unlike break
else: #assuming last case
State_Dict[state_key].append(line) #line has already been stripped
print(State_Dict)
答案 1 :(得分:0)
与您的代码有关的问题是,当您出现“!”时,您正在停止寻找某个州的城市。但城市的新部分也以“州”开头的线表示。 另一个错误是您每次都从城市开始迭代访问城市,而不是从当前所在的州开始访问。
State_Dict = {}
main_fd = '''\
State Texas
Austin
Houston
Dallas
State Florida
Orlando
Miami
Jacksonville
Naples
!
State California
San Diego
Los Angeles
San Francisco\
'''.splitlines()
for idx, mystate in enumerate(main_fd):
if "State" in mystate:
state_key = "_".join(mystate.split()[1:])
State_Dict[state_key] = []
for cities in main_fd[idx+1:]:
if '!' in cities or "State" in cities:
break
else:
State_Dict[state_key].append(cities.rstrip())
print(State_Dict)
输出:
{'Florida': ['Orlando', 'Miami', 'Jacksonville', 'Naples'],
'California': ['San Diego', 'Los Angeles', 'San Francisco'],
'Texas': ['Austin', 'Houston', 'Dallas']}