3

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

4 Answers 4

9
>>> 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']
Sign up to request clarification or add additional context in comments.

Comments

1

If ast is available, you can use ast.literal_eval.

Comments

1

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.

Comments

0

Use re module to split your string into needed elements. For example

re.findall("u\'([^\']+)\'", u"[u'38', u'36', u'34', u'32']")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.