Unicode - String - list Manipulation

Shashi picture Shashi · Apr 13, 2011 · Viewed 7.7k times · Source

I have a data s = u"[u'38', u'36', u'34', u'32']" which has data type unicode i want to make this data as simple list of element like s= ['38','36','32'],
i try to use simplejson.loads but its not working simple json work with the ('["s"]') this type of string not ("['s']") so any buddy please guide me to get of this problem

thanks in advance

Answer

eumiro picture eumiro · Apr 13, 2011
>>> import ast
>>> s = u"[u'38', u'36', u'34', u'32']"
>>> [ item.encode('ascii') for item in ast.literal_eval(s) ]
['38', '36', '34', '32']