3 ways to parse time strings in python [Note]

What you want to do

I want to make a memorandum about how to parse the time written in text to the datetime type of python.

environment

Background

    1. The date and time appearing in the XML of OpenWeatherMap was in a format I had never seen (only for myself) and the time was GMT (UTC). 2.9 It is difficult to deal with the situation where the dates are different due to the time difference unless the process of adding 9.9 hours is performed.
    1. I thought it would be best to put it in a pre-prepared mold and calculate the time difference additionally.

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))

result

Method Time required
parse() 5.8433
strptime() 1.0622
String cutout 0.2875

Summary

parse () is easy but slow with one shot It takes a lot of time to cut out character strings, but it is fast.

The site that I used as a reference

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

result screen of jupyter notebook

image.png

Recommended Posts

3 ways to parse time strings in python [Note]
6 ways to string objects in Python
Parse XML in Python
A clever way to time processing in Python
Note to daemonize python
Compare strings in Python
Reverse strings in Python
To represent date, time, time, and seconds in Python
Convert timezoned date and time to Unixtime in Python2.7
Parse a JSON string written to a file in Python
How to measure processing time in Python or Java
Note assigning image textures to materials in Maya python
Things to note when initializing a list in Python
Multi-digit multiplication time up to 300 million digits in python
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
I want to manipulate strings in Kotlin like Python!
To flush stdout in Python
Login to website in Python
Search for strings in Python
Parse User Agent in Python
Python Input Note in AtCoder
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Various ways to calculate the similarity between data in python
How to generate exponential pulse time series data in python
Allow Python to select strings in input files from folders
[Python] How to do PCA in Python
[Note] Project Euler in Python (Problem 1-22)
Convert markdown to PDF in Python
How to collect images in Python
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
Measure function execution time in Python
In the python command python points to python3.8
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Try to calculate Trace in Python
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
Python (from first time to execution)
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
Foreign Key in Python SQLite [Note]
How to use PubChem in Python
5 Ways to Create a Python Chatbot
Code tests around time in Python
How to handle Japanese in Python
An alternative to `pause` in Python
Extract strings from files in Python
Determine the date and time format in Python and convert to Unixtime
How to write offline real time Solve E04 problems in Python
Various ways to create an array of numbers from 1 to 10 in Python.
[Note] AI / machine learning / python related websites [updated from time to time]
How to calculate "xx time" in one shot with Python timedelta
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
How to access environment variables in Python