开发者

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'],

i try to use simplejson.loads but its not working simple json work with the ('["s"]') this type of string not ("['s'开发者_Go百科]") so any buddy please guide me to get of this problem

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:

  1. JSON doesn't allow single-quote strings, '38', only double-quote, "38".
  2. 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']")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜