大家好,我是一个学习python的新手,我只想打印当前时间以x开头的内容(例如,如果当前时间以= 4开始,则打印" Hi ",time = 4:18),这是我编写的代码,它显示属性错误:
import datetime
local = datetime.datetime.now().time().replace(microsecond=0)
if local.startswith('16'):
print("Hi! It's ", local)发布于 2019-08-06 22:23:50
.replace()方法返回一个date对象。date对象没有.startswith()方法。该方法仅适用于str。
请先尝试将日期转换为字符串:
if str(local).startswith('16'):
print("Hi! It's ", local)The documentation列出了date对象上所有可用的方法。
https://stackoverflow.com/questions/57378200
复制相似问题