Python日期时间Strptime错误:'-'是格式为'%-m-%-d-%y%-H:%M:%S'的错误指令 [英] Python Datetime Strptime Error: '-' is a bad directive in format '%-m-%-d-%y %-H:%M:%S'

查看:689
本文介绍了Python日期时间Strptime错误:'-'是格式为'%-m-%-d-%y%-H:%M:%S'的错误指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过类似的问题,但是它们似乎与日期时间处理(或不处理)时区的方式有关。

I know that there have been similar questions asked, but they seemed to have to do with the way datetime deals (or doesn't deal) with timezones.

设置有点复杂,可能与问题无关,但我认为按原样包含代码很重要,因此要有一点背景知识:

The setup is a little complicated, and probably not relevant to the problem, but I thought it was important to include the code as is, so a little background:

I'我有一个数组字典。这些数组中的每一个都代表同一个人的尝试,但发生在不同的时间。最终,我将寻找这些日期中最早的日期。这可能是一个回旋解决方案,但是我将所有日期都转换为datetime对象,找到最早的对象,然后仅使用该索引进行首次尝试:

I've got a dictionary of arrays. Each of these arrays represents an "attempt" by the same person, but taking place at different times. Ultimately I'm going to be looking for the earliest of these dates. This may be a bit of a roundabout solution, but I'm converting all of the dates to datetime objects, finding the earliest and then just using that index to pull out the first attempt:

这是设置尝试日期时间数组的代码,如下所示:

Here's what the code looks like to setup that array of attempt datetimes:

for key in duplicates_set.keys():
    attempt_dates = [datetime.strptime(attempt['Attempt Date'], "%-m-%-d-%y %-H:%M:%S") for attempt in duplicates_set[key]]

以下是原始日期字符串之一的格式:

Here's the format of what one of the original date strings looks like:

2016/12/5下午3:27:58

我是什么m返回:

ValueError: '-' is a bad directive in format '%-m-%d-%y %-H:%M:%S'

我认为这是指在' m','d'和'H',因为它们是非零填充的小数。为什么会告诉我?

I assume that's referring to the dashes placed before the 'm', 'd' and 'H' because they're non-zero-padded decimals. Why is it telling me that?

推荐答案

%-* -跳过填充-是GNU libc扩展。它不是POSIX strftime的一部分,因此不能保证可移植到最终不由GNU strftime C库函数支持的时间格式调用的系统中。

%-* -- to skip padding -- is a GNU libc extension. It's not part of POSIX strftime, and thus not guaranteed to be portable to systems where your time-formatting calls aren't eventually backed by GNU's strftime C library function.

Python datetime 模块文档明确指定了它支持的格式字符串,并且未给出此扩展名。因此,尽管GNU date和GNU strftime()支持此功能,但Python datetime 却不提供此功能。

The Python datetime module documentation explicitly specifies the format strings it supports, and this extension is not given. Thus, while this is supported in GNU date and GNU strftime(), it isn't available in Python datetime.

这篇关于Python日期时间Strptime错误:'-'是格式为'%-m-%-d-%y%-H:%M:%S'的错误指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆