在Python中摆脱日期字符串的前导零? [英] Get rid of leading zeros for date strings in Python?
本文介绍了在Python中摆脱日期字符串的前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的示例中,我想获得12/1/2009的日期字符串的前导零点作为回报而不是12/01/2009。我想我可以使用正则表达式。但对我来说,似乎是过度杀戮。有更好的解决方案吗?
>>> time.strftime('%m /%d /%Y',time.strptime('12 / 1/2009','%m /%d /%Y'))
'12 / 01/2009'
另见
解决方案
@OP,做一点字符串操作并不需要太多的时间。
>>> t = time.strftime('%m /%d /%Y',time.strptime('12 / 1/2009','%m /%d /%Y'))
>> ; '/'。join(map(str,map(int,t.split(/)))
'12 / 1/2009'
Is there a nimble way to get rid of leading zeros for date strings in Python?
In the example below I'd like to get 12/1/2009 in return instead of 12/01/2009. I guess I could use regular expressions. But to me that seems like overkill. Is there a better solution?
>>> time.strftime('%m/%d/%Y',time.strptime('12/1/2009', '%m/%d/%Y'))
'12/01/2009'
See also
解决方案
@OP, it doesn't take much to do a bit of string manipulation.
>>> t=time.strftime('%m/%d/%Y',time.strptime('12/1/2009', '%m/%d/%Y'))
>>> '/'.join( map( str, map(int,t.split("/")) ) )
'12/1/2009'
这篇关于在Python中摆脱日期字符串的前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文