简体   繁体   中英

How to convert string to list in python. Like "['a']' to ['a']

I have a list from a database in the form of a string, and I want to convert this string to a list. For example if I get '["a","b"]' , I want to convert it into this: ["a","b"] .

I've tried list() method.

userf = user.pending
print(userf)
#userf is "['animation']"
userf.append(tosend).save()

Use literal_eval

import ast

l = '["a","b"]'

ast.literal_eval(l)

output

['a', 'b']

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