I want to make a memorandum about how to parse the time written in text to the datetime type of python.
By the way, the date that appears in XML looks like this.
2017-07-08T19:33:11
I found a lot when I looked up the method of parsing, so I will summarize the code.
import time
from datetime import datetime
from dateutil.parser import parse
start = time.time()
date="2017-07-08T19:33:11" #Character string to be parsed
for i in range(100000): #parse()Loop to parse 100,000 times
dt1 = parse(date)
elapsed_time_for_parse = time.time() - start #parse()Stores the elapsed time when parsing with
start = time.time()
for i in range(100000): #strptime()Loop to parse 100,000 times
dt2 = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
elapsed_time_for_strptime = time.time() - start #strptime()Stores the elapsed time when parsing with
start = time.time()
for i in range(100000): #Loop that parses 100,000 times by cutting out a character string
dt3 = datetime(
year=int(date[0:4]),month=int(date[5:7]),day=int(date[8:10]),
hour=int(date[11:13]),minute=int(date[14:16]),second=int(date[17:20])
)
elapsed_time_for_cutparse = time.time() - start #Stores the elapsed time when parsing with character string cutout
print('parse() takes {:.4f}sec, dt1:{}'.format(elapsed_time_for_parse,dt1))
print('strptime() takes {:.4f}sec, dt2:{}'.format(elapsed_time_for_strptime,dt2))
print('cut and parse takes {:.4f}sec, dt3:{}'.format(elapsed_time_for_cutparse,dt3))
Method | Time required |
---|---|
parse() | 5.8433 |
strptime() | 1.0622 |
String cutout | 0.2875 |
parse () is easy but slow with one shot It takes a lot of time to cut out character strings, but it is fast.
OpenWeatherMap Code 7 ward Date and time strings are parsed with python. Extract the year, month and hour miyalog Python datetime.strptime is very slow
Recommended Posts