I have a text file that contains a smaller dataset(taken from csv file) like so -
2020-05-24T10:44:37.613168#[ 0. 0. -0.06210425 0. ]
2020-05-24T10:44:37.302214#[1. 1. 0. 0.]
2020-05-24T10:44:36.192222#[0. 0. 0. 0.]
Then read from it using
data = f.readlines()
for row in data:
img_id, label = row.strip("\n").split("#")
where in label is a string list which looks like
[ 0. 0. -0.24604772 0. ]
[ 0. 0. -0.24604772 0. ]
[1. 1. 0. 0.]
I'd like to convert each string element to float. However, the square brace []
and decimal .
preventing me from converting.
Tried so far -
Removing
[]
so -label = label[1:-1]
but I would need them as an array later. Then doing thisprint([list(map(float, i.split())) for i in label])
resulted in errorValueError: could not convert string to float: '.'
Using
ast.literal_eval
.label = ast.literal_eval(row.strip("\n").split("#"))
. GettingValueError: malformed node or string: ['2020-05-24T10:57:52.882241 [0. 0. 0. 0.]']
Referred
Need to read string into a float array
Cannot convert list of strings to list of floats in python using float()
How do you convert a list of strings to a list of floats using Python?
Convert list of strings to numpy array of floats
So,
- What else should I try in order to convert them to float array which is iterable? Or what am I doing wrong? Should I have to remove the square braces?
- If I can make things much easier, how can I store the data in txt file? Is CSV better than txt in this case?
- I need to extend this logic to 110,000 entries. Will any of steps cause problems then?
Thank you. Any help will be greatly appreciated. Please help.