[PYTHON] 0 Convert unfilled date to datetime type with regular expression

python's datetime has a function strptime () that converts a string type to a datetime type, but it cannot convert a string that is not padded with 0s. (2020/04/03 can be converted, but 2020/4/3 cannot)

Note that there was an easy alternative.

If full-width characters are included, use some method to make full-width characters half-width and then execute. There are various methods, so choose the one you like.

code

Use the datetime constructor instead of strptime (). https://docs.python.org/ja/3/library/datetime.html#datetime.datetime

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

import datetime
import re

#Get a list of numbers contained in a string
l = re.findall(r"\d+", text)

#Make a list of strings a list of numbers
l = [int(s) for s in l]

#Specify the date and time in the datetime type constructor (expand the list and pass it)
date = datetime.datetime(*l)

sample

import datetime
import re

text = "2020/4/2"
l = re.findall(r"\d+", text)
# l : ["2020", "4", "2"]

l = [int(s) for s in l]
# l : [2020, 4, 2]

date = datetime.datetime(*l)
# date = datetime.datetime(2020, 4, 2)

bonus

May be used for conversion from Japanese calendar to Western calendar (It is necessary to correspond to the "first year")

import datetime
import re

text = "April 3, 2nd year of Reiwa"

diff = 0
if text[:2]=="Reiwa":
    diff = 2018
else:
    # hoge
    pass

l = re.findall(r"\d+", text)
l = [int(s) for s in l]

l[0] += diff
date = datetime.datetime(*l)

Recommended Posts

0 Convert unfilled date to datetime type with regular expression
Receive date type (datetime) with ArgumentParser [python]
Convert Windows epoch values to date with python
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
Convert pandas dataframe elements to regular string type
Convert 202003 to 2020-03 with pandas
Regular expression with pymongo
Date notation regular expression
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
[python] Convert date to string
Regular expression manipulation with Python
Convert .ipynb to .html (with BatchFile)
Convert list to DataFrame with python
I tried to make a regular expression of "date" using Python
Convert sentences to vectors with gensim
Decompose hostname with co.jp with regular expression
String replacement with Python regular expression
Scikit-learn DecisionTreeClassifier with datetime type values
Introduction to regular expression processing system
How to calculate date with python
Convert PDF to image with ImageMagick
Misunderstanding due to datetime type subtraction
I tried to convert datetime <-> string with tzinfo using strftime () and strptime ()
Convert memo at once with Python 2to3
[Django] Convert QuerySet to dictionary type list
Convert from PDF to CSV with pdfplumber
Convert character strings to features with RoBERTa
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert FX 1-minute data to 5-minute data with Python
[Introduction to cx_Oracle] (17th) Date type handling
Convert PDF files to PNG files with GIMP
Convert array (struct) to json with golang
Convert Python date types to RFC822 format
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
Convert DICOM to PNG with Ascending and Descending
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert mp4 to mp3 with ffmpeg (thumbnail embedded version)
Convert svg file to png / ico with Python
Easily convert Jupyter Notebooks to blogs with fastpages
How to write regular expression patterns in Linux
How to handle datetime type in python sqlite3
[Python] How to compare datetime with timezone added
Convert strings to character-by-character list format with python
Change the string to be replaced according to the matched string by replacing with Python regular expression