下面的是已經用re.sub採集出來的了,需要在每個 {} 前面加上 “1”: “2”: “3”,請問該如何寫這個:
'{"nClass":-1,"nIcon":16},{"nClass":-1,"nIcon":15},{"nClass":-1,"nIcon":14}'
最終效果:
'“1”:{"nClass":-1,"nIcon":16},“2”:{"nClass":-1,"nIcon":15},“3”:{"nClass":-1,"nIcon":14}'
淡淡烟草味2017-05-18 11:00:27
你描述的不夠清楚,你說結果應該是一個list,你是要轉換成dict嗎?
import json
str = '{"nClass":-1,"nIcon":16},{"nClass":-1,"nIcon":15},{"nClass":-1,"nIcon":14}'
lst = json.loads('[{}]'.format(str))
l = ['"{}":{}'.format(i+1, json.dumps(v)) for i,v in enumerate(lst)]
print ','.join(l)