s = "[3339:1.6101369,1062:1.5,5751:1.5,6376:1.5]" s = s.strip("[]") # Drop the brackets for kv in s.split(","): key, value = kv.split(":") print key, value
Alternatively, you can convert this to a dictionary (after removing the brackets):
d = dict(kv.split(":") for kv in s.split(","))
and then iterate through the dictionary:
for key in d: print key, d[key]
source share