I have a list of dates written as 20130710.0
problem I'm graphing the dates so they need to remain in a list so I don't quite know how to take what code I do know (for converting one at a time) to convert the whole string
Here's my list example:
[20130710.0, 20130802.0, 20130806.0, 20130807.0, 20130809.0]
And here is how I am converting just a single value from the list
from datetime import datetime, timedelta
intDate = "20130713.0"
ActDate = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
So I need my full list of float dates changed to actual dates basically. What am I missing?
EDIT: Added an example list
Just use a list comprehension :
In [10]: intDates = ["20130713.0", "20130715.0", "20130718.0"]
In [11]: actDates = [datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8])) for s in intDates]
In [12]: actDates
Out[12]:
[datetime.datetime(2013, 7, 13, 0, 0),
datetime.datetime(2013, 7, 15, 0, 0),
datetime.datetime(2013, 7, 18, 0, 0)]
Use a list comprehension, and use datetime.strptime()
to parse your values (converted to strings):
intDates = [20130710.0, 20130802.0, 20130806.0]
[datetime.strptime(format(d, '.0f'), '%Y%m%d') for d in intDates]
Demo:
>>> from datetime import datetime
>>> intDates = [20130710.0, 20130802.0, 20130806.0]
>>> [datetime.strptime(format(d, '.0f'), '%Y%m%d') for d in intDates]
[datetime.datetime(2013, 7, 10, 0, 0), datetime.datetime(2013, 8, 2, 0, 0), datetime.datetime(2013, 8, 6, 0, 0)]
I used format()
to format the floats as a string without the decimal point for ease of parsing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.