4

Given a unicode object:

u'[obj1,obj2,ob3]' 

How do you convert it to a normal list of objects?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
mossplix
  • 3,783
  • 2
  • 26
  • 31
  • 2
    What sort of objects were you hoping to make the list contain? – Ignacio Vazquez-Abrams Feb 15 '11 at 08:40
  • i subclassed the django.forms.widget class .. and the value_from_datadict method returns a list of objects as a unicode string .. i want to be able to get the list of objects not the unicode string.. – mossplix Feb 15 '11 at 08:54
  • 1
    Suspect this question is based on a misconception, ie from outputting the results in the terminal... – Daniel Roseman Feb 15 '11 at 10:30
  • 1
    -1: Fix the question: "django.forms.widget class .. and the value_from_datadict method returns a list of objects " That's the real question. Please delete this mess and start again with the **real** question. – S.Lott Feb 15 '11 at 10:45

4 Answers4

7
import ast
s = u'[obj1,obj2,ob3]'
n = ast.literal_eval(s)
n
[obj1, obj2, ob3]
Eklavya
  • 86
  • 1
  • 2
4

Did you mean this? Converting a unicode string to a list of strings. BTW, you need to know the encoding when dealing with unicode. Here I have used utf-8

>>> s = u'[obj1,obj2,ob3]'
>>> n = [e.encode('utf-8') for e in s.strip('[]').split(',')]
>>> n
['obj1', 'obj2', 'ob3']
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • when object.__repr__ has been called ... is there a way to reverse it? how do you get the objects not the string representations? – mossplix Feb 15 '11 at 08:56
  • @mossplix, Yes, use eval for that purpose. >>>obj=repr(True);obj2=eval(obj);print obj2 – Senthil Kumaran Feb 15 '11 at 09:02
  • @mossplix: No, there isn't. `repr()` has no fixed format, and can basically return an arbitrary representation of the objects contents. –  Feb 15 '11 at 09:04
  • 3
    `repr()` isn't required to return a representation of the objects in valid python syntax, and for the majority of objects it actually doesn't. Basically only primitive types and standard containers have a `repr()`, which is valid Python syntax, and for these types, one should better use `ast.literal_eval()` instead of `eval()` for security reasons. –  Feb 15 '11 at 09:08
  • Yes, I agree to usage of `ast.literal_eval` instead of `eval` for security reasons. Thanks for adding this. Yes agree to your other comment too. But for most common purposes, repr returns a string which be eval'ed back and there are instances when it wont – Senthil Kumaran Feb 15 '11 at 09:17
0

When unicode data doesn't show unicode u...

On exporting data from an excel table using openpyxl, my unicode was invisible. Use print repr(s) to see it

>>>print(data)
>>>print(type(data))
["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]
<type 'unicode>
>>>print repr(data)
u'["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]'

The fix:

>>>import ast    
>>>data = ast.literal_eval(entry)
>>>print(data)
>>>print(type(data))
["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]
<type 'list'>
Matt Allan
  • 33
  • 5
0

What you posted is a unicode string. To encode it e.g. as UTF-8 use yourutf8str = yourunicodestr.encode('utf-8')

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636