Ad
How To Format Multiple Date Formats Into Single Date In Python
I have a list in Python with different date formats:
list1 = ["30-4-1994", "1994-30-04", "30/04/1994",
"30-apr-1994", "30/apr/1994","1994-30-apr"]
I want to format multiple date formats into a single date like dd-mm-yyyy
How can I do that?
Ad
Answer
In an ideal world, you know the format of your inputs.
Where this is not possible, I recommend you use a 3rd party library for mixed format dates.
Two libraries that come to mind are dateutil
(via dateutil.parser.parse
) and pandas
(via pandas.to_datetime
). Below is an example implementation with the former.
Note the only occasion when parser.parse
was unsuccessful had to be covered with a manual conversion via datetime.strptime
. datetime
is part of the standard Python library.
from datetime import datetime
from dateutil import parser
list1 = ["30-4-1994", "1994-30-04", "30/04/1994",
"30-apr-1994", "30/apr/1994","1994-30-apr"]
def converter(lst):
for i in lst:
try:
yield parser.parse(i)
except ValueError:
try:
yield parser.parse(i, dayfirst=True)
except ValueError:
try:
yield datetime.strptime(i, '%Y-%d-%b')
except:
yield i
res = list(converter(list1))
# [datetime.datetime(1994, 4, 30, 0, 0),
# datetime.datetime(1994, 4, 30, 0, 0),
# datetime.datetime(1994, 4, 30, 0, 0),
# datetime.datetime(1994, 4, 30, 0, 0),
# datetime.datetime(1994, 4, 30, 0, 0),
# datetime.datetime(1994, 4, 30, 0, 0)]
You can then format into strings any way you like using datetime.strptime
:
res_str = [i.strftime('%d-%m-%Y') for i in res]
# ['30-04-1994',
# '30-04-1994',
# '30-04-1994',
# '30-04-1994',
# '30-04-1994',
# '30-04-1994']
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad