Skip to main content
 首页 » 编程设计

python之在 Python 中,如何将字符串拆分为多个整数

2025年05月04日18me-sa

这行得通!

>>> from datetime import date 
 
>>> today=date(2011,10,11) 

但是我该怎么做呢?

>>> day =  '2011/10/11' 
 
>>> today=date(day.split('/')) 

注意:

>>> day.split('/')  
 
['2011', '10', '11'] 

我看过this链接。但是我需要 date() 的整数而不是列表

请您参考如下方法:

使用datetime.datetime.strptime() ,这是专门为解析日期而设计的:

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