Reputation: 51
I have a text file with its contents written as follows:
State Texas
Austin
Houston
Dallas
State Florida
Orlando
Miami
Jacksonville
Naples
!
State California
San Diego
Los Angeles
San Francisco
Goal: I want to read from this text file and turn them into dictionary and should look like this -
state_dict = {
'Texas': ['Austin', 'Houston', 'Dallas'],
'Florida': ['Orlando', 'Miami', 'Jacksonville', 'Naples'],
'California': ['San Diego', 'Los Angeles', 'San Francisco']
}
So far my code is this:
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)
But the output is this:
{
'Texas': [
' Austin', ' Houston', ' Dallas',
'State Florida', ' Orlando', ' Miami',
' Jacksonville', ' Naples'
],
'California': [
' San Diego', ' Los Angeles', ' San Francisco'
]
}
How do I fix this?
Upvotes: 0
Views: 69
Reputation: 6246
What you are looking for is something like this. Consider the loop iterating through one line at a time, and build a series of checks for your conditions. PS. Let me know if this doesn't work, wrote it without testing.
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)
Upvotes: 2
Reputation: 3612
The problems with you code were that you were stopping looking for cities of a state when you occurred "!" but new portion of cities also were indicated by line starting with "State". Another bug was that you iterated for cities from beginning every time instead of state you're currently at.
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)
output:
{'Florida': ['Orlando', 'Miami', 'Jacksonville', 'Naples'],
'California': ['San Diego', 'Los Angeles', 'San Francisco'],
'Texas': ['Austin', 'Houston', 'Dallas']}
Upvotes: 0