Skip to content
Advertisement

Using datetime.strptime when data is a little messy : extra spaces, Jan or January

Currently the text I am dealing with are dates with a somewhat standard format, however the data isn’t super clean.

For example the text can be in these formats:

JavaScript

I’m not quite sure how to deal with this. I want to convert these strings into 2021-01-01 format.

My plan was to convert to datetime object, and then convert back to string.

However when using strptime, the pattern seemingly needs to be rigid, and doesn’t allow for regex like patterns.

JavaScript

instead of something like '%b|%Bs[.,]?

Anyone have suggestions as to how to convert my text into year-month-day format?

Advertisement

Answer

You can try using the dateutil library, (it’s one of the most downloaded pypi packages)

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement