code_junkie

Splitting a semicolon-separated string to a dictionary, in Python

Nov 11th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. "Name1=Value1;Name2=Value2;Name3=Value3"
  2.  
  3. dict = {
  4. "Name1": "Value1",
  5. "Name2": "Value2",
  6. "Name3": "Value3"
  7. }
  8.  
  9. s= "Name1=Value1;Name2=Value2;Name3=Value3"
  10. dict(item.split("=") for item in s.split(";"))
  11.  
  12. >>> s = "Name1='Value=2';Name2=Value2;Name3=Value3"
  13.  
  14. >>> dict(csv.reader([item], delimiter='=', quotechar="'").next()
  15. for item in csv.reader([s], delimiter=';', quotechar="'").next())
  16.  
  17. {'Name2': 'Value2', 'Name3': 'Value3', 'Name1': 'Value1=2'}
  18.  
  19. >>> import urlparse
  20. >>> urlparse.parse_qs("Name1=Value1;Name2=Value2;Name3=Value3")
  21. {'Name2': ['Value2'], 'Name3': ['Value3'], 'Name1': ['Value1']}
Add Comment
Please, Sign In to add comment