简体   繁体   中英

Remove quotes from list of dictionaries in python

How can I remove the single quotes from the following so it can be recognized as a list of one dictionary object as apposed to a list of one string object? Given that it is a list, I cannot use pd.replace.

['{"PLAYER":"Player Name","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']

You can use ast.literal_eval :

import ast
s = ['{"PLAYER":"Player Name","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']
final_s = [ast.literal_eval(i) for i in s]

Output:

[{'SALARY': '0000.00', 'REC RATE': '62.50%', 'OPP': 'CI', 'YPT': '8.52', 'TAR': '64', 'GP': '5', 'PLAYERID': '12322', 'WEEK 3': '14', 'POS': 'BR', 'ARDS': '545', 'WEEK 2': '11', 'PLAYER': 'Player Name', 'SCHEDULE_ID': '40623', 'POW TAR': '32.99%', 'WEEK 4': '9', 'TEAM': 'IT', 'RZTAR': '6', 'REC': '40', 'WEEK 5': '19'}]

just use eval() for the purpose

s=['{"PLAYER":"PlayerName","SALARY":"0000.00","OPP":"CI","POS":"BR","TEAM":"IT","SCHEDULE_ID":"40623","PLAYERID":"12322","GP":"5","TAR":"64","RZTAR":"6","POW TAR":"32.99%","WEEK 2":"11","WEEK 3":"14","WEEK 4":"9","WEEK 5":"19","ARDS":"545","YPT":"8.52","REC":"40","REC RATE":"62.50%"}']

s = [eval(item) for item in s]

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