Tuesday 15 January 2013

How to parse `key1:val1,val2,val3,key2:val1,val2` in python? -


string

key1: val1, val2, val3, key2: val11, val22 The most efficient way to split:

  key1: ['val1', 'val2', 'val3'] key2: ['val11', 'val22']  < / Pre> 

thanks

 in  [150]: answer = {} 151]: In pairs for [152]: s.split (','): s = "key1: val1, val2, val3, key2: val11, val22": k, _, v = pair.rpartition (": ") If k: key = k if the key is not in answer: Answer [key] = [] Answer [key] .append (v) .....: in [153]: for k, vals in the north. ['Val1', 'val2', 'val3']  

No comments:

Post a Comment