简体   繁体   中英

replace text from list of dictionary python

I am trying to convert

list1=["{\"username\":\"abhi\",\"pass\":2087}"]

to

list1=[{"username":"abhi","pass":2087}]

Is there any way to do it without lots of splitting and creating dictionary and then putting into list?

You can parse it as a JSON string.

import json
list2 = [json.loads(item) for item in list1]

Output -

[{u'pass': 2087, u'username': u'abhi'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM