【发布时间】:2018-09-11 15:04:01
【问题描述】:
我正在尝试从 pickle 文件中获取日期并将日期添加一天。打印语句返回 2018-09-10 。然后在datetime.datetime.strftime(dataHist['last_updated'], '%Y-%m-%d') 行上出现错误'strftime' requires a 'datetime.date' object but received a 'str'
Import pickle
dataHistFile = open('dat.pkl', 'rb')
dataHist = pickle.load(dataHistFile, encoding='bytes')
print(dataHist['last_updated'])
dt_obj = datetime.datetime.strftime(dataHist['last_updated'], '%Y-%m-%d')
date = dt_obj + datetime.timedelta(days=1)
2018-09-10
文件“C:/用户/Arvinth Kumar/Downloads/strtsmrt-master/gendata.py”,第 81 行,在 init 中 fetchData() 文件“C:/Users/Arvinth Kumar/Downloads/strtsmrt-master/gendata.py”,第 15 行,在 fetchData news.init() 文件“C:\Users\Arvinth Kumar\Downloads\strtsmrt-master\news.py”,第 58 行,在 init getNews() 文件“C:\Users\Arvinth Kumar\Downloads\strtsmrt-master\news.py”,第 38 行,在 getNews dt_obj = datetime.datetime.strftime(dataHist['last_updated'], '%Y-%m-%d') TypeError: 描述符 'strftime' 需要一个 'datetime.date' 对象但收到了 'str'
请帮忙!
【问题讨论】:
-
你对
type(dataHist)有什么看法?和type(dataHist['last_updated'])? -
我想你想要strptime()