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

标签 python string integer

这行得通!

>>> 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)

关于python - 在 Python 中,如何将字符串拆分为多个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8080794/

相关文章:

c++ - 对数组中的整数进行排序。但它不适用于某些情况

python - 错误 : Could not find a version that satisfies the requirement scikit-learn==0. 21.0

python - 使用 scipy.io.savemat 保存嵌套列表

python - 使用 openCV 打开视频

java - http请求得到的字符串会保存在常量池中吗?

c++ - 如何修改 lambda 中的字符串 vector

python - 使用装饰器在定义的函数上产生 NameError

c - 字符串数组访问错误

python - 从列表列表中删除特定类的所有元素

python - Python 中的 %i 和 %d 有什么区别?