I was wondering how you could convert the following list such that the strings contained in the list are properly formatted.
["'34872'", "'Johnathan'", "'Bonzo'", "'100'"]
Ideal output:
["34872", "Johnathan", "Bonzo", "100"]
I was wondering how you could convert the following list such that the strings contained in the list are properly formatted.
["'34872'", "'Johnathan'", "'Bonzo'", "'100'"]
Ideal output:
["34872", "Johnathan", "Bonzo", "100"]
You can use ast.literal_eval
in a list comprehension
>>> from ast import literal_eval
>>> data = ["'34872'", "'Johnathan'", "'Bonzo'", "'100'"]
>>> [literal_eval(i) for i in data]
['34872', 'Johnathan', 'Bonzo', '100']
Otherwise you can use simple string replacement
>>> [i.replace("'", "") for i in data]
['34872', 'Johnathan', 'Bonzo', '100']