Python ValueError:时间数据不匹配

Python ValueError:时间数据不匹配,python,python-3.x,python-datetime,Python,Python 3.x,Python Datetime,所以我提出了这个错误 ValueError:时间数据“8/16/2016 9:55”与格式“%m/&d/%Y”不匹配 %H:%M' 我知道%m是月份的两位数格式(零填充)。正如我们所看到的,“8”(8月)没有零填充。这就是这个错误的问题吗?我如何解决这个问题 将日期时间导入为dt 结果_列表=[] 对于ask_帖子中的 result_list.append([a[6],int(a[4])) 按小时计数={} 注释按小时={} 日期\格式=“%m/&d/%Y%H:%m” 对于结果列表中的行: 日期

所以我提出了这个错误

ValueError:时间数据“8/16/2016 9:55”与格式“%m/&d/%Y”不匹配 %H:%M'

我知道%m是月份的两位数格式(零填充)。正如我们所看到的,“8”(8月)没有零填充。这就是这个错误的问题吗?我如何解决这个问题

将日期时间导入为dt
结果_列表=[]
对于ask_帖子中的
result_list.append([a[6],int(a[4]))
按小时计数={}
注释按小时={}
日期\格式=“%m/&d/%Y%H:%m”
对于结果列表中的行:
日期=行[0]
注释=行[1]
time=dt.datetime.strtime(日期,日期格式).strftime(“%H”)
```我只想提取时间```
如果时间不是按小时计数:
按小时[时间]计数=1
评论按小时[时间]=评论
其他:
按小时[时间]计数+=1
评论按小时[时间]+=评论

您的日期格式%not&

import datetime as dt
result_list = []
for a in ask_posts:
    result_list.append([a[6], int(a[4])])
counts_by_hour = {}
comments_by_hour = {}
date_format = '%m/%d/%Y %H:%M' #  change & with %

    for row in result_list:
        date = row[0]
        comment = row[1]
        time = dt.datetime.strptime(date, date_format).strftime("%H")
        ``` I want  to extract the Hour only```
        if time not in counts_by_hour:
            counts_by_hour[time] = 1
            comments_by_hour[time] = comment
        else:
            counts_by_hour[time] += 1
            comments_by_hours[time] += comment

我的错。非常感谢你。