How to get the datetime from a string containing '2nd' for the date in Python? -


I have some strings from which I want to get a date time. They are formatted like this:

  Thursday 2 May 2013 1900  

I know how can I change it in a routine, its Besides, I'm having trouble with "2 ND " now I have the following

  gt; & Gt; Datetime.strptime ('Thursday 02 May 2013 19:00', '% a% d% B% Y% H:% M') datetime.datetime (2013, 5, 2, 19, 0)   

That works fine with zero padded numbers for the day of the month, but when I try 2 , it returns a ValueError:

< Pre> & gt; ; & Gt; & Gt; Traceback (most recent call final): File "& lt; Input & gt;", in line1, in: 2 May 2013 19:00 ', 2 May 2013 1 9:00', '% a% D% B% Y% H:% M ') & lt; Module & gt; (Data_string, format)) ValueError: Time data '2 May 2013 1 9:00' Format 'does not match% a% d% B% Y% H:% M'

I can not find anything related to the prices (1, 2, 3 etc.) for dates, do anyone know how I can do it? Any suggestion will be appreciated! Consider using

.

This is a third party library, a powerful parser that can control these types of things. Dateutil.parser import parse s = 'Thursday 2 May 2013 1 9:00' d = parse (s) from

  print (D, type) (d)) # 2013-05-02 19:00:00 & lt; Class 'datetime.datetime' & gt;  

A brief warning (actually not in your case): If there is no aspect of your date in the datereal string (say that you leave Given out of the month) then this will be the default for the default argument with the current date 00:00:00 with time it is the default. If you are required with a separate datetime object, it is obvious that overwrite it.

The easiest way to install dateutil might be to command install pipe dragon-date .


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -