Python日期时间与格式字符串不匹配

问题描述

我正在尝试解析特定格式的日期

这是我的日期字符串2020-08-12 00:00:00 +05:30
格式字符串:%Y-%m-%d %H:%M:%S %Z

执行格式化的代码:

date_object = datetime.datetime.strptime(date_string,format_str)

输出: ValueError: time data '2020-08-12 00:00:00 +05:30' does not match format '%Y-%m-%d %H:%M:%S %Z'

我尝试过的事情: 将区域从大写字母更改为小z,格式字符串为%Y-%m-%d %H:%M:%S %z

Python版本:3.6

解决方法

我尝试使用小写字母z,并为我工作,例如:

for (int num_lorries = 0; num_lorries < options; num_lorries++) {
    int remaining_cargo = cargo - lorrysize * num_lorries;
    int num_vans = remaining_cargo / vansize;
    // Exercise for you: figure out why this bit is needed.
    // (consider remaining_cargo=25 and vansize = 10)
    if (remaining_cargo % vansize > 0) {
        num_vans++;
    }
    Solution sol(num_lorries,num_vans);
    // Do something with sol
}

打印

import datetime

date_string="2020-08-12 00:00:00 +05:30"
format_str="%Y-%m-%d %H:%M:%S %z"

date_object = datetime.datetime.strptime(date_string,format_str)


print('Date-time:',date_object)

编辑

我正在使用Python 3.7,因此Sam的评论解释了为什么它对我有用。

查看Guides,确实可以从时区中删除冒号,例如 代替

Date-time: 2020-08-12 00:00:00+05:30
date_string="2020-08-12 00:00:00 +05:30"

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...