在Python中,如何将字符串拆分为多个整数? [英] In Python, how do I split a string to a number of integers?

查看:176
本文介绍了在Python中,如何将字符串拆分为多个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做有效!

>>> from datetime import date

>>> today=date(2011,10,11)

但我该怎么做?

>>> day =  '2011/10/11'

>>> today=date(day.split('/'))

注意:

>>> day.split('/') 

['2011', '10', '11']

我见过这个 link.But我需要整数日期()而不是列表

I have seen this link.But i need integers for the date() not a list

推荐答案

使用 datetime.datetime.strptime() ,专门用于解析日期:

Use datetime.datetime.strptime(), which is designed specifically for parsing dates:

In [5]: datetime.datetime.strptime('2011/12/03', '%Y/%m/%d').date()
Out[5]: datetime.date(2011, 12, 3)

这篇关于在Python中,如何将字符串拆分为多个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆