I have a string that looks like this:
"Name1=Value1;Name2=Value2;Name3=Value3"
Is there a built-in class/function in Python that will take that string and construct a dictionary, as though I had done this:
dict = {
"Name1": "Value1",
"Name2": "Value2",
"Name3": "Value3"
}
I have looked through the modules available but can’t seem to find anything that matches.
Thanks, I do know how to make the relevant code myself, but since such smallish solutions are usually mine-fields waiting to happen (ie. someone writes: Name1=’Value1=2′;) etc. then I usually prefer some pre-tested function.
I’ll do it myself then.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There’s no builtin, but you can accomplish this fairly simply with a generator comprehension:
s= "Name1=Value1;Name2=Value2;Name3=Value3"
dict(item.split("=") for item in s.split(";"))
[Edit] From your update you indicate you may need to handle quoting. This does complicate things, depending on what the exact format you are looking for is (what quote chars are accepted, what escape chars etc). You may want to look at the csv module to see if it can cover your format. Here’s an example: (Note that the API is a little clunky for this example, as CSV is designed to iterate through a sequence of records, hence the .next() calls I’m making to just look at the first line. Adjust to suit your needs):
>>> s = "Name1='Value=2';Name2=Value2;Name3=Value3"
>>> dict(csv.reader([item], delimiter='=', quotechar="'").next()
for item in csv.reader([s], delimiter=';', quotechar="'").next())
{'Name2': 'Value2', 'Name3': 'Value3', 'Name1': 'Value1=2'}
Depending on the exact structure of your format, you may need to write your own simple parser however.
Method 2
This comes close to doing what you wanted:
>>> import urlparse
>>> urlparse.parse_qs("Name1=Value1;Name2=Value2;Name3=Value3")
{'Name2': ['Value2'], 'Name3': ['Value3'], 'Name1': ['Value1']}
Method 3
s1 = "Name1=Value1;Name2=Value2;Name3=Value3"
dict(map(lambda x: x.split('='), s1.split(';')))
Method 4
It can be simply done by string join and list comprehension
",".join(["%s=%s" % x for x in d.items()])
>>d = {'a':1, 'b':2}
>>','.join(['%s=%s'%x for x in d.items()])
>>'a=1,b=2'
Method 5
easytiger $ cat test.out test.py | sed 's/^/ /'
p_easytiger_quoting:1.84563302994
{'Name2': 'Value2', 'Name3': 'Value3', 'Name1': 'Value1'}
p_brian:2.30507516861
{'Name2': 'Value2', 'Name3': "'Value3'", 'Name1': 'Value1'}
p_kyle:7.22536420822
{'Name2': ['Value2'], 'Name3': ["'Value3'"], 'Name1': ['Value1']}
import timeit
import urlparse
s = "Name1=Value1;Name2=Value2;Name3='Value3'"
def p_easytiger_quoting(s):
d = {}
s = s.replace("'", "")
for x in s.split(';'):
k, v = x.split('=')
d[k] = v
return d
def p_brian(s):
return dict(item.split("=") for item in s.split(";"))
def p_kyle(s):
return urlparse.parse_qs(s)
print "p_easytiger_quoting:" + str(timeit.timeit(lambda: p_easytiger_quoting(s)))
print p_easytiger_quoting(s)
print "p_brian:" + str(timeit.timeit(lambda: p_brian(s)))
print p_brian(s)
print "p_kyle:" + str(timeit.timeit(lambda: p_kyle(s)))
print p_kyle(s)
Method 6
IF your Value1, Value2 are just placeholders for actual values, you can also use the dict() function in combination with eval().
>>> s= "Name1=1;Name2=2;Name3='string'"
>>> print eval('dict('+s.replace(';',',')+')')
{'Name2: 2, 'Name3': 'string', 'Name1': 1}
This is beacuse the dict() function understand the syntax dict(Name1=1, Name2=2,Name3='string'). Spaces in the string (e.g. after each semicolon) are ignored. But note the string values do require quoting.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0