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

时间:2020-08-13 12:36:03

标签: python date datetime python-3.6 python-datetime

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

这是我的日期字符串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

1 个答案:

答案 0 :(得分:1)

我尝试使用小写字母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"