Python converts formatted string to list

I have the string "[u'foo']" (Yes, it includes square brackets and u'' ). I have to convert this to a list that looks like [u'foo'] .

list("[u'foo']") will not work.

Any suggestions?

0
source share
2 answers
 >>> import ast >>> s = "[u'foo']" >>> ast.literal_eval(s) [u'foo'] 

documentation

+17
source
 eval("[u'foo']", {'__builtins__':[]}, {}) 
+1
source

Source: https://habr.com/ru/post/1386126/


All Articles