简体   繁体   中英

Split string into dictionary with space delimiter

I have a Python string like this

query = 'name:Thoong company:"Big Company, LLC" age:25'

I want convert it to a dictionary with key and value split by ':'

{
    'name': 'Thoong',
    'age': 25,
    'company': 'Big Company, LLC',
}

I tried to split by ' ' and then ':' . but seems the query.split(' ') not work because we have ' ' "Big Company, LLC"

Is there any way to archive this, could be using a regular expression?

Use shlex :

import shlex

dict(i.split(":") for i in shlex.split(query))

Output:

{'age': '25', 'company': 'Big Company, LLC', 'name': 'Thoong'}

Use regex and insert into a dictionary:

import re

regex_1 = r"(\w+:\w+)"
regex_2 = r"(\w+:\"\w+\s\w+,\s\w+\")"
query = 'name:Thoong company:"Big Company, LLC" age:25'
r_1 = re.findall(regex_1, query)
r_2 = re.search(regex_2, query).group(1)
d = {*r_1,
     r_2}

print(d)

Returning:

{'name:Thoong', 'age:25', 'company:"Big Company, LLC"'}

How about this one? This looks not elegant but you can easily understand what this code does.

query = 'name:Thoong company:"Big Company, LLC" age:25'
tmp = query.split(":")
keys = []
values = []

# extract keys
for t in tmp:
    _t = t.split(" ")
    if len(_t) == 1:
        keys.append(_t[0])
    else:
        values.append(" ".join(_t[:-1]))
        keys.append(t.split(" ")[-1])

values.append(keys[-1])
del keys[-1]
result = {k: v for k, v in zip(keys, values)}
print(result)

Splitting on blanks ( ) and other signs probably won't work as names may contain any number of characters.

If the query always consists of these three elements, you could try splitting at the keywords:

d = {'name': query.split('name:')[1].split(' company:')[0],
     'company': query.split('company:')[1].split(' age:')[0],
     'age': query.split('age:')[1]}

If the query is ill formatted, though, this will not work. There, you might have to try to parse it and escape the blanks and colons that occur within quotation marks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM