I have this string
'[334.0, 223.0, 41.0, 819.0]'
And I need to transform this in this array:
[334.0, 223.0, 41.0, 819.0]
Any ideas? Thanks
I have this string
'[334.0, 223.0, 41.0, 819.0]'
And I need to transform this in this array:
[334.0, 223.0, 41.0, 819.0]
Any ideas? Thanks
Use the ast module.
Ex:
import ast
print(ast.literal_eval('[334.0, 223.0, 41.0, 819.0]'))
output:
[334.0, 223.0, 41.0, 819.0]
Simple one-liner with no extra imports:
a = '[334.0, 223.0, 41.0, 819.0]'
b = [ float(i) for i in a[1:-1].split(',') ]
print b
Output:
[334.0, 223.0, 41.0, 819.0]
What about eval
?
That would evaluate a string as if it is a python code.
For example:
string='[334.0, 223.0, 41.0, 819.0]'
a = eval(string)
print(a[0])
output:
334.0