python - 将日期时间对象列表转换为字符串
问题描述
我想将日期时间对象列表转换为字符串,但对如何完成此任务感到困惑。
hm=dt.date(2013,1,2)
t=hm.strftime('%m/%d/%Y')
t
这产生:'01/02/2013'
但是,当我将变量更改为日期列表时,它会引发错误。
hm=[dt.date(2013,1,1), dt.date(2013,1,2)]
t=hm.strftime('%m/%d/%Y')
t
错误:list indices must be integers or slices, not datetime.date
我是否需要使用某种for
循环来完成此任务?
解决方案
您需要遍历列表以将每个单独的日期转换为string
:
>>> t = [d.strftime('%m/%d/%Y') for d in hm]
或者,如果您想要将所有日期转换为由其他一些(假设)string
连接的字符串,您也可以这样做:string
,
>>> s = ', '.join(t)
推荐阅读
- c++ - 尝试在 OpenGL 3 中进行鼠标拾取时获取不正确的向量
- java - 使用 jpa 标准 api 进行复杂过滤
- python - Selenium 没有针对 XPATH Python 选择值
- python - URLS 循环以获取财务数据
- javascript - 从单独的回调中获取 Promise 解决方案
- c# - 如何在 Quartz.NET 作业中注入 NavigationManager?
- c# - 如何在 wix 中实现安装程序进度条的回滚?
- azure-devops-extensions - 如果任何 AzureDevops 构建代理不可用,如何获得警报?
- c# - 单击按钮时显示 2 个用户输入变量的结果
- python - 在linux中升级python