python regular increment
phpcn_u1582
phpcn_u1582 2017-05-18 10:58:27
0
2
592

The following has been collected using re.sub. You need to add "1": "2": "3" in front of each {}. How to write this:

'{"nClass":-1,"nIcon":16},{"nClass":-1,"nIcon":15},{"nClass":-1,"nIcon":14}'

final effect:

'“1”:{"nClass":-1,"nIcon":16},“2”:{"nClass":-1,"nIcon":15},“3”:{"nClass":-1,"nIcon":14}'
phpcn_u1582
phpcn_u1582

reply all(2)
淡淡烟草味

Your description is not clear enough. You said the result should be a list. Do you want to convert it to a 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)
黄舟
import re
data = '{"nClass":-1,"nIcon":16},{"nClass":-1,"nIcon":15},   {"nClass":-1,"nIcon":14}'
# 提取为数组
data = re.findall('((?=,\s?)?\{.*?\})+', data)
# 下标从1开始, 所以i+1
print ','.join(['"%d":%s' % (i+1, v) for i, v in enumerate(data)])
# "1":{"nClass":-1,"nIcon":16},"2":{"nClass":-1,"nIcon":15},"3":{"nClass":-1,"nIcon":14}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template