Skip to content
Advertisement

Convert String (with other string in) in Date Object – Python

I’m trying to convert this French string : ’20 avril 2018 – Arrivée définitive’ as a Date Object.

Jour = ['20 avril 2018 - Arrivée définitive', '21 septembre 2018 - Arrivée définitive', '1 décembre 2019 - Arrivée définitive']

I tried :

import dateparser # $ pip install dateparser

for date_string in Jour:
    print(dateparser.parse(date_string).date())

But I have this error :

AttributeError: 'NoneType' object has no attribute 'date'

Thank you in advance for your help.

I’m hoping that this will help someone.

Advertisement

Answer

As you can read in documentation The parse function returns datetime representing parsed date if successful, else returns None so while looping, the parse function returned None of which you tried to use date function causing an error.

I am not sure if it would help but If you check in console:

>>> import dateparser
>>> dateparser.parse("20 avril 2018 - Arrivée définitive")
>>> dateparser.parse("21 septembre 2018 - Arrivée définitive")
>>> dateparser.parse("21 septembre 2018")
datetime.datetime(2018, 9, 21, 0, 0)
>>>

So string “21 septembre 2018” parses well but “21 septembre 2018 – Arrivée définitive” parses as None (nothing printed means None in this case)

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