0

I have this string from a file:

[u'Some string']

I read it as a string in my python script, and I need to convert it back into a list. This looks like a horrible idea, but so far eval seems to work fine:

>>> eval("[u'Some string']")
[u'Some string']
>>> type(eval("[u'Some string']"))
<type 'list'>

But this seems like a terribly horrible idea. Any way to elegantly do this?

Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144

1 Answers1

3

eval is unsafe. However, just use ast.literal_eval

>>> import ast
>>> s = "[u'Some string']"
>>> ast.literal_eval(s)
[u'Some string']
>>> 

This is safe.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172