>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']
ast.literal_eval:
Answer from Roger Pate on Stack OverflowEvaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans,
NoneandEllipsis.This can be used for evaluating strings containing Python values without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
python - How to convert list to string - Stack Overflow
converting a string that is list-like into a list
what is the difference between list and list[str]?
Strings are lists?
What are the different methods to convert a list to a string in Python?
Are there any other Python built-in functions to convert lists to strings?
Why would I need to convert a list to a string in Python?
Videos
>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']
ast.literal_eval:
Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans,
NoneandEllipsis.This can be used for evaluating strings containing Python values without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
The json module is a better solution whenever there is a stringified list of dictionaries. The json.loads(your_data) function can be used to convert it to a list.
>>> import json
>>> x = '[ "A","B","C" , " D"]'
>>> json.loads(x)
['A', 'B', 'C', ' D']
Similarly
>>> x = '[ "A","B","C" , {"D":"E"}]'
>>> json.loads(x)
['A', 'B', 'C', {'D': 'E'}]
Hopefully I can explain this properly. I have no control over how the input string which is kind of formatted like a list of lists but without quotes (so I can't use json.loads)
Example input (I'm reading it in using sys.argv):
"[[a,1],[b,2]]"
I'd like to convert this to be a real list of lists so I can iterate through them in my script.
I can get what I need using split but I was wondering if there was a better way to do it. Either a better way to divide them or a way to add quotes - whatever works.
import sys
try1 = sys.argv[1]
lst1 = try1.split("],")
print("try #1 (works but could be better?)")
print(len(lst1))
for item in lst1:
print(item.lstrip("[[").rstrip("]]"))
# doesn't work - 1 item in the list
print("\n\nTry #2")
try2 = sys.argv[1:]
print(len(try2))
# doesn't work - length of 13
print("\n\nTry #3")
print(len(list(sys.argv[1])))Output
$ python3 t.py "[[a,1],[b,2]]" try #1 (works but could be better?) 2 a,1 b,2 Try #2 1 Try #3 13
Thanks for reading