将python字符串日期转换为mysql datetime [英] convert python string date to mysql datetime
本文介绍了将python字符串日期转换为mysql datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串字段女巫是从互联网上刮下来的日期,如下所示:
Lun Ene 27,2014 9:52 am,2014年3月2日,下午5点38分,...
Lun =星期几(lunes / monday)
Ene =月(enero / january)
我需要输入一个mysql表,在datetime
'YYYY-MM-DD HH:MM:SS'
/ pre>
我想象这是一个很常见的问题,并且想知道是否有人已经有脚本来做,或者可以指出我可以在哪里寻找...
提前感谢
解决方案
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]
if int爆炸[2] [: - 1])< 10:
day_of_the_month =%02d%(int(explode [2] [: - 1]))
如果explode [5] =='am':
time_split = explode [4] .split(':')
如果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(':'
如果在范围(1,12)中的int(time_split [0]):
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:00I 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 = month (enero/january)
I need to enter them in a mysql table, in a datetime field.
'YYYY-MM-DD HH:MM:SS'
I imagine it is a very common issue and was wondering if someone already have a script to do it or could point out where I could look for...
Thanks in advance!
解决方案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] if int(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(':') if int(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这篇关于将python字符串日期转换为mysql datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文