fork download
  1. import re
  2. rx = r'(\w+)\s+("[^"]*"|[^\s;]+)'
  3. s = 'cheese "stilton";pigeons 17; color "blue"; why "because I said so";'
  4. result = {}
  5. for key,val in re.findall(rx, s):
  6. if val.startswith('"') and val.endswith('"'):
  7. val = val[1:-1]
  8. result[key]=val
  9.  
  10. print(result)
  11.  
  12.  
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
{'why': 'because I said so', 'color': 'blue', 'cheese': 'stilton', 'pigeons': '17'}