python - 'h' 是格式为 '%m/%d/%Y %h:%M' 的错误指令
问题描述
试图让它工作很长时间,不知道为什么它不会让我将字符串更改为日期时间,我是 Python 新手,请帮忙。
counts_by_hour = {}
comment_by_hour = {}
for row in result_list3:
date = "10/15/2015 16:38"
date2 = dt.datetime.strptime(date, "%m/%d/%Y %h:%M")
ValueErrorTraceback (most recent call last)
<ipython-input-19-98628a0af3bb> in <module>()
4 for row in result_list3:
5 date = "10/15/2015 16:38"
----> 6 date2 = dt.datetime.strptime(date, "%m/%d/%Y %h:%M")
7
8
/usr/lib/python3.4/_strptime.py in _strptime_datetime(cls, data_string, format)
498 """Return a class cls instance based on the input string and the
499 format string."""
--> 500 tt, fraction = _strptime(data_string, format)
501 tzname, gmtoff = tt[-2:]
502 args = tt[:6] + (fraction,)
/usr/lib/python3.4/_strptime.py in _strptime(data_string, format)
327 del err
328 raise ValueError("'%s' is a bad directive in format '%s'" %
--> 329 (bad_directive, format)) from None
330 # IndexError only occurs when the format string is "%"
331 except IndexError:
ValueError: 'h' is a bad directive in format '%m/%d/%Y %h:%M'```
解决方案
%H
使用了几个小时。因此,您的代码应如下所示:
date2 = dt.datetime.strptime(date, "%m/%d/%Y %H:%M")
推荐阅读
- git - Git 设置网址来源
- c# - c# Windows Media 播放组件冻结
- python - I want to create django popup form in my project
- visual-studio-code - 在 Visual Studio Code 中找不到更改 Python 扩展语言的选项
- javascript - javascript 检查电话号码和地址是否为空白
- python - 从包的所有文件中导入所有类
- c# - 如何在单独的线程上闯入从 Python.NET 调用的 Python 代码?
- android - Kotlin lambda function with arguments
- c++ - 递归定义元组
- forms - 使用 RestSharp 调用 TLS 1.2 服务器在控制台应用程序中工作,而不是在 Xamarin 表单中