Home > Enterprise >  Convert a unified string to dictionary
Convert a unified string to dictionary

Time:05-29

I'm looking for the way to convert string data that looks like the following:

"Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"

to a 'dict' with two keys

{'city':'Kiev', 'population': 123}

I can convert substrings of the original string to lists of strings in this format:

[['Kyiv', 'region A', 123'], ['Kyiv', 'region B', '456'],] ...

but can't proceed further.

Could somebody help me parse this string?

This is the code I have so far:

dic_city = {}

cities = []
population = []

cities = [a.split(',') for a in s.split('\n')]

print(cities)

CodePudding user response:

Split the data by newline to get each entry, and then split by comma space to get the city name and population from each entry:

entries = data.split("\n")
result = []
for entry in entries:
    city, _, pop = entry.split(", ")
    result.append(dict(city=city, population=population))
    
print(result)

This outputs:

[
 {'city': 'Kyiv', 'population': '123'},
 {'city': 'Kyiv', 'population': '456'},
 {'city': 'Kyiv', 'population': '789'},
 {'city': 'Poltava', 'population': '321'},
 {'city': 'Poltava', 'population': '876'},
 {'city': 'Poltava', 'population': '654'}
]

CodePudding user response:

txt = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"

d = {}
for x in txt.splitlines():
  city = x.split(',')[0]
  if d.get(city):
    d[city]  = int(x.split(',')[2]) # same city then add population
  else:
    d[city] = int(x.split(',')[2]) # different city add new key to dictionary

print(d)

Output -

{'Kyiv': 1368, 'Poltava': 1851}

CodePudding user response:

Try this

# split into nested lists
aux = [x.split(', ') for x in s.split('\n')]
# build a list of dictionaries
res = [{'city':c, 'population': int(p)} for c, _, p in aux]
res

The same code as a comprehension

s = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
# create a dictionary in loop
[{'city':c, 'population': int(p)} for c, _, p in (x.split(', ') for x in s.split('\n'))]

#[{'city': 'Kyiv', 'population': 123},
# {'city': 'Kyiv', 'population': 456},
# {'city': 'Kyiv', 'population': 789},
# {'city': 'Poltava', 'population': 321},
# {'city': 'Poltava', 'population': 876},
# {'city': 'Poltava', 'population': 654}]
  • Related