Convert DD / MM / YYYY HH: MM: SS to MySQL TIMESTAMP

I would just like to find and reformat the text of the format "DD / MM / YYYY" to "YYYY / MM / DD" so that it is compatible with MySQL TIMESTAMPs, in the list of text elements that may or may not contain the atall date, under python. (I think RegEx?)

Basically I am looking for a way to check the list of items and fix any found time formats.

The great thing about standards is that there are so many to choose from ....

+3
source share
2 answers

You can read a string into an object datetimeand then output it as a string using a different format. For instance,

>>> from datetime import datetime
>>> datetime.strptime("31/12/2009", "%d/%m/%Y").strftime("%Y/%m/%d")
'2009/12/31'

Basically I am looking for a way to check the list of items and fix any found time formats.

, , dateutil.

>>> from dateutil.parser import parse
>>> parse("31/12/2009").strftime("%Y/%m/%d")
'2009/12/31'

dateutil . map parse .

+1

MySQLdb ( "mysql-python" ), datetime timestamp datetime . , , .

Python 2.5 :

from datetime import datetime

value = datetime.strptime(somestring, "%d/%m/%Y")

python , .

import time
from datetime import datetime

timetuple = time.strptime(somestring, "%d/%m/%Y")
value = datetime(*timetuple[:6])

, C. man strptime unix, . , .

. , MySQL , . , , "" . , - pytz.

+1

Source: https://habr.com/ru/post/1764209/


All Articles