I know this has been asked many times, however, I cannot seem to find one that works for me.
I've got the following text using requests.
data = response.text
var racePostTime = ["","2024-01-21 13:00:00","2024-01-21 13:30:00","2024-01-21 14:00:00","2024-01-21 14:30:00","2024-01-21 15:00:00","2024-01-21 15:35:00","2024-01-21 16:05:00","2024-01-21 16:40:00","2024-01-21 17:15:00","2024-01-21 17:50:00"];
And I've been able to get the data i want using re.
posttime = re.findall('(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})', data)
print(posttime)
Getting the below list of tuples.
[('2024', '01', '21', '13', '00', '00'), ('2024', '01', '21', '13', '30', '00'), ('2024', '01', '21', '14', '00', '00'), ('2024', '01', '21', '14', '30', '00'), ('2024', '01', '21', '15', '00', '00'), ('2024', '01', '21', '15', '35', '00'), ('2024', '01', '21', '16', '05', '00'), ('2024', '01', '21', '16', '40', '00'), ('2024', '01', '21', '17', '15', '00'), ('2024', '01', '21', '17', '50', '00')]
This is where i'm stuck. I tried the following but got an error.
race1 = datetime(posttime[0])
Just like @jasonharper has mentioned, datetime.datetime.strptime
is more commonly used.
from datetime import datetime
s = '2024-01-21 13:00:00'
print(datetime.strptime(s, '%Y-%m-%d %H:%M:%S')) # %Y for year in 4 digits
So for your case, which you get a JavaScript array, you can simply use json
library to do the parsing for you.
import json
data = data.split('=')[1][:-1] # Take the list and eliminate the ';' at the end
arr = json.loads(data) # Parse string to python list
print(arr) # ['', '2024-01-21 13:00:00', ...]
Now you get a list of strings of date and time. That's it. Do anything you want with them.