I have a list for birth data each record has 3 columns for [DOB, weight, height] as such:
JavaScript
x
3
1
bd = [['10/03/2021 00:00','6.2', '33.3'],['12/04/2021 00:00','6.2', '33.3'],
2
['13/05/2021 00:00','6.2','33.3']]
3
I need to change the data types of this as they are all strings in the list I want the first item in the record to be datetime and then the rest to be floats. I have:
JavaScript
1
6
1
newdata = [i[0] for i in bd]
2
p= []
3
for x in bd:
4
xy = datetime.datetime.strptime(x,'%d/%m/%Y %H:%M') # this works to change the data type
5
p.append(xy) #it fails to update this to the new list
6
I get an attribute error:
JavaScript
1
2
1
AttributeError: 'str' object has no attribute 'append'
2
I would like to achieve this just by utilizing pythons file IO operations. I also want to maintain each record of data together in a list within the main list I just want to update the datatypes.
Advertisement
Answer
Your code is incomplete, there may be unexpected variable coverage, you can try to use the list comprehension directly.
JavaScript
1
2
1
[[datetime.datetime.strptime(i[0],'%d/%m/%Y %H:%M'), float(i[1]), float(i[2])] for i in bd]
2