I need cast a list to a string and get back the string to a list. There\'s a python way to make this behavior?
l1 = [\'aa\',\'bb\',\'cc\']
s = str(l1)
l2 = c
In somewhat recent Python versions, you can use ast.literal_eval, which is essentially eval without the security problems. You could also try to do the parsing yourself (or use Python's parser and then replicate the logic of literal_eval
before evaling the AST), although both are wheel reinventions and the latter is likely much less robust as soon as it gets to string literals.
Why do you need it anyway? There are serialization formats that can handle conversion to and from string of various data structures (not just lists of strings) for you, such as Pickle (somewhat insecure itself, read the notice in the docs), JSON, YAML, and probably more. They're much more robust and appropriate for such tasks.