I read a list of lists from a txt file and I got a list like this:
JavaScript
x
2
1
["[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793]", "[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]"]
2
Each list item in this list is a str type which is a problem.
it should be like this:
JavaScript
1
2
1
[[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793], [21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]]
2
How do I do that?
Advertisement
Answer
Using the eval()
function along with a list comprehension we can try:
JavaScript
1
4
1
inp = ["[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793]", "[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]"]
2
output = [eval(x) for x in inp]
3
print(output)
4
This prints:
JavaScript
1
5
1
[
2
[21049090, 'AARTIIND22AUGFUT', 'AARTIIND', 850, 1793],
3
[21049346, 'ABB22AUGFUT', 'ABB', 250, 3329]
4
]
5