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 = cast_string_to_list(s)
print l2
"['aa','bb','cc']"
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 = cast_string_to_list(s)
print l2
"['aa','bb','cc']"
Use a serialization library like json:
import json
l1 = ['aa','bb','cc']
s = json.dumps(l1)
l2 = json.loads(s)
print s
print l1 == l2
pickle over json is that pickle can handle many more classes of objects. For instance, import datetime; json.dumps(datetime.datetime.now()) won't work unless you define a helper function that knows how to serialize datetime objects.json.loads() doesn't create an ordered list. Does anybody know a solution? @KirkStrauser @lctr30 @MikeGraham @JochenRitzelIn 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.
You can use ast module:
import ast
l1 = ['aa','bb','cc']
s = str(l1)
ast.literal_eval(s)
>>> ['aa','bb','cc']
Or something like this - but i don't like it too much:
l1 = ['aa','bb','cc']
s = str(l1)
l2 = [x.strip(" '") for x in s.strip('[]').split(',')]
The pickle module is probably what you're looking for:
import pickle
l1 = ['aa','bb','cc']
s = pickle.dumps(l1)
l2 = pickle.loads(s)
print l2
pickle is tremendously convenient and powerful. Don't avoid such useful mechanisms just because it's possible to use them inappropriately.PickleErrors when it can't pickle something. Pickle has great coverage of Python datatypes (much better than JSON). Basically, it's a poor choice for things like accepting serialized data from external clients. It's an excellent choice for when you have control over both endpoints of the transaction.This is an aside for this question, but I was looking for a way to cast a non-delimited string to a single-element list and realized I could assign with square brackets. Knowing this behavior in Python helped me think through string-to-list and back problems.
l1 = 'aa bb cc'
print(type(l1))
l2 = [l1]
print(type(l2))
print(l2)
print(l2[0])
If you are always working with a list, then the following kind of thing should work just fine. Beware, it will not cast ints to ints and such; they will always be string items in the list.
l1 = ['aa','bb','cc']
s = str(l1)
l2 = s[1:-1].split(',')
print l2
['aa', 'bb', 'cc']
You probably want to check the boundaries of the string and such to check that it's long enough for that array stuff too...