Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串转换为24小时标记处的日期/小时:ValueError未转换的数据仍然为:4_Python_Date_Type Conversion_Extract_Strptime - Fatal编程技术网

Python 将字符串转换为24小时标记处的日期/小时:ValueError未转换的数据仍然为:4

Python 将字符串转换为24小时标记处的日期/小时:ValueError未转换的数据仍然为:4,python,date,type-conversion,extract,strptime,Python,Date,Type Conversion,Extract,Strptime,我试图从以下字符串中提取日期和小时: string = '3/24/2016 24' # 24 is the hour 使用以下代码: result = datetime.strptime(string, '%m/%d/%Y %H') 但是,我遇到以下错误消息: ValueError:未转换的数据仍然存在:4 潜在客户:StrTime是否不将24视为一小时(0->23对1->24)?如果是这样的话,既然它是一个字符串,我应该如何修复它 我假设要处理24的拐角情况,您可以将日期部分解析

我试图从以下字符串中提取日期和小时:

string = '3/24/2016 24'     # 24 is the hour
使用以下代码:

result = datetime.strptime(string, '%m/%d/%Y %H')
但是,我遇到以下错误消息: ValueError:未转换的数据仍然存在:4
潜在客户:StrTime是否不将24视为一小时(0->23对1->24)?如果是这样的话,既然它是一个字符串,我应该如何修复它

我假设要处理24的拐角情况,您可以将日期部分解析为日期,然后替换“hour”mod 24,例如:

d, t = '3/24/2016 24'.partition(' ')[::2]
dt = datetime.strptime(d, '%m/%d/%Y').replace(hour=int(t) % 24)
# datetime.datetime(2016, 3, 24, 0, 0)

我假设要处理24的情况,可以将日期部分解析为日期,然后替换“hour”mod 24,例如:

d, t = '3/24/2016 24'.partition(' ')[::2]
dt = datetime.strptime(d, '%m/%d/%Y').replace(hour=int(t) % 24)
# datetime.datetime(2016, 3, 24, 0, 0)