Python 从文件中读取并提取一些内容以转到字典

Python 从文件中读取并提取一些内容以转到字典,python,Python,我有一个文本文件,它是这样写的: things to do blah blah blah Places to visit: ! State Texas Austin Houston Dallas State Florida Orlando Miami Jacksonville Naples ! State California San Diego Los Angeles San Francisco I can only pick one each year ! State Illinois Ch

我有一个文本文件,它是这样写的:

things to do
blah blah blah
Places to visit:
!
State Texas
Austin
Houston
Dallas
State Florida
Orlando
Miami
Jacksonville
Naples
!
State California
San Diego
Los Angeles
San Francisco
I can only pick one each year
!
State Illinois
Chicago
Peoria
Rockford
! 
Traceback (most recent call last):
snip 
snip   
State_Dict[state_key].append(line.rstrip())
NameError: name 'state_key' is not defined
目标:我希望能够提取任何一行,以一个状态作为键开始,随后的行作为值城市,直到它到达一个值!或者另一个州

下面的代码来自于我从另一个帖子中获得的帮助,但它只在文本文件中没有其他垃圾时起作用。我希望代码只选择州和城市

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):
        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:
        State_Dict[state_key].append(line) #line has already been stripped
此代码与上面编写的文本文件一起输出时出现如下错误:

things to do
blah blah blah
Places to visit:
!
State Texas
Austin
Houston
Dallas
State Florida
Orlando
Miami
Jacksonville
Naples
!
State California
San Diego
Los Angeles
San Francisco
I can only pick one each year
!
State Illinois
Chicago
Peoria
Rockford
! 
Traceback (most recent call last):
snip 
snip   
State_Dict[state_key].append(line.rstrip())
NameError: name 'state_key' is not defined

请告知。谢谢

当您看到时,重置状态非常重要! 我希望这不是家庭作业:

State_Dict = {}
state_key = None  # init state key with empty value
with open('state.txt', 'r') as main_fd:
    for line in main_fd:
        line = line.strip()  # left and right stripped

        if line.startswith('State '):  # in python there is no need for "c-like" brackets in "if"; also I included a space
            # are you trying to replace spaces with "_"?
            state_key = "_".join(line.split()[1:])
            # if we see same state second time, what should we do?
            State_Dict[state_key] = []

        elif line.startswith('!'):
            state_key = None  # important! resent state after "!"
            # no need for next line as we go to next iteration anyway
            # continue # goes to next iteration of loop instead of stopping the loop unlike break

        elif state_key:  # if state_key is None, it is evaluated as False
            State_Dict[state_key].append(line)  # line has already been stripped

您应该使用line.startswithState,而不是在中使用。我也一样!。而且,埃利夫总是正确的。如果以前从未设置过state_key,那么在这种情况下它是什么呢?对不起,我输入了else的编辑版本:我实际上是一名网络工程师,正在尝试自动化,所以你可以说它是Python世界中的noob。我现在用我需要应用的真实数据集测试代码。看起来不错,但我正在审核每一行。所以,这是有报酬的家庭作业:在某些技术方面,我们都是noobs,感谢保持网络连接!再次检查,你们并没有重复状态。还有一件事——当你们陷入困境,互联网上的人嘲笑你们时,这会有所帮助。