Unicode - String - list Manipulation
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']
,
thanks in advance
>>> 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']
If ast
is available, you can use ast.literal_eval
.
Well the problem is that that string is not valid JSON syntax. It is valid Python syntax, but not JSON, for two reasons:
- JSON doesn't allow single-quote strings,
'38'
, only double-quote,"38"
. - JSON doesn't allow a u before the string,
u"38"
, only bare strings which are implicitly Unicode,"38"
.
You need to either change the input format, or use something which can process Python strings instead.
You could use eval
, which reads strings containing Python syntax, but note that this is highly dangerous if you are accepting arbitrary input, since someone can supply code to execute. Nevertheless, it works:
>>> eval(u"[u'38', u'36', u'34', u'32']")
[u'38', u'36', u'34', u'32']
Edit: khachik's answer is probably better than eval
, since it won't be susceptible to evaluating arbitrary Python code, only reading Python data structures.
Use re
module to split your string into needed elements. For example
re.findall("u\'([^\']+)\'", u"[u'38', u'36', u'34', u'32']")
精彩评论