Convert Python String Date To Mysql Datetime
I have a string field witch is a date scraped from internet and reads like this: 'Lun Ene 27, 2014 9:52 am', 'Mar Feb 11, 2014 5:38 pm',... Lun= day of week (lunes/monday) Ene =
Solution 1:
month_of_the_year = ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dec']
def convert_to_mysql_format(string):
explode = string.split()
day_of_the_month = explode[2][:-1]
ifint(explode[2][:-1]) < 10:
day_of_the_month = "%02d" % (int(explode[2][:-1]),)
if explode[5] == 'am':
time_split = explode[4].split(':')
if time_split[0] == '12':
time_split[0] = '00'
elif int(time_split[0]) < 10:
time_split[0] = "%02d" % int(time_split[0])
else:
time_split = explode[4].split(':')
ifint(time_split[0]) in range(1, 12):
time_split[0] = str(int(time_split[0]) + 12)
if month_of_the_year.index(explode[1]) < 12:
explode[1] = "%02d" % (month_of_the_year.index(explode[1])+1)
return explode[3]+'-'+explode[1]+'-'+day_of_the_month+' '+time_split[0]+':'+time_split[1]+':00'print convert_to_mysql_format("Lun Ene 27, 2014 9:52 am")
print convert_to_mysql_format("Lun Ene 27, 2014 9:52 pm")
2014-01-27 09:52:00 2014-01-27 21:52:00
Solution 2:
By default, Python runs using C locale:
>>>from datetime import datetime>>>datetime.strptime("Tue Feb 11, 2014 5:38 PM", "%a %b %d, %Y %I:%M %p")
datetime.datetime(2014, 2, 11, 17, 38)
>>>import locale>>>locale.nl_langinfo(locale.T_FMT_AMPM)
'%I:%M:%S %p'
Changing locale partially helps on my system:
>>> locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
'es_ES.UTF-8'
>>> datetime.strptime("Lun Ene 27, 2014 9:52 am"[:-2], "%a %b %d, %Y %I:%M %p")
datetime.datetime(2014, 1, 27, 9, 52)
>>> locale.nl_langinfo(locale.T_FMT_AMPM)
''
T_FMT_AMPM
is not set for some reason on my system for es_ES.UTF-8
. To fix it, you could manually add 12 hours if the time string ends with 'pm'
.
The strftime()
and time
behaviour is the same.
Note: the locale name may be different on other systems.
Post a Comment for "Convert Python String Date To Mysql Datetime"