'e'在格式'%e %B %Y'中是一个糟糕的指令

时间:2021-02-03 21:32:37

I'm trying to convert a string given in the "DD MM YYYY" format into a datetime object. I expect the day integer in my output to consist of a single digit if the day in the date is in single digits. For example, the input '1 May 2014' should be converted to '1 May 2014' after converting into a datetime object instead of '01 May 2014'.

我正在尝试将“DD MM yyyyy”格式的字符串转换为datetime对象。我希望输出中的日整数包含一个个位数,如果日期中的日是个位数的话。例如,输入“2014年5月1日”应在转换为datetime对象而不是“2014年5月01日”后转换为“2014年5月1日”。

For this purpose, I have used the %e format for converting the day in my output date. Here's the code for the same:

为此,我使用了%e格式来转换输出日期中的日期。下面是同样的代码:

import datetime
from datetime import timedelta

s = "1 July 2013"
d = datetime.datetime.strptime(s, "%e %B %Y")  

print d.strftime("%e %B %Y")

However, I'm getting the following error:

但是,我得到了以下错误:

ValueError: 'e' is a bad directive in format '%e %B %Y'

What's wrong with the formatting ? I'm using Python 2.7, if that helps.

格式有什么问题吗?我用的是Python 2。7,如果有用的话。

3 个解决方案

#1


5  

%e is not a format code guaranteed to be processable by Python's datetime.strftime. Consult the table here for the list of format codes Python is guaranteed to support. However, since CPython's strftime calls the underlying C library's strftime, on some OS's, such as Linux, the %e format code exists.

%e不是保证可以通过Python的datetime.strftime处理的格式代码。请参阅这里的表格,了解Python所支持的格式代码列表。但是,由于CPython的strftime调用了底层C库的strftime,在某些操作系统(如Linux)上,%e格式代码是存在的。


The CPython source code says,

CPython的源代码说,

Other codes may be available on your platform. See documentation for\n\ the C library strftime function.\n"

其他代码可以在您的平台上使用。请参阅有关C库strftime函数的文档。


There is no other format code (guaranteed to exist) which behaves like %e. So if your system does not have %e or you want your code to be cross-platform compatible, use string formatting, as shown by Zacrath or Jon Clements.

没有其他格式代码(保证存在)的行为与%e类似。因此,如果您的系统没有%e,或者希望代码能够跨平台兼容,请使用字符串格式,如Zacrath或Jon Clements所示。

#2


4  

You can intermix string formatting and attribute access with the strftime style using str.format, eg:

可以使用string .format将字符串格式和属性访问与strftime样式混合使用,例如:

from datetime import datetime

s = "1 July 2013"
d = datetime.strptime(s, "%d %B %Y")
 # 2013-07-01 00:00:00
out = '{0.day} {0:%B %Y}'.format(d)
# 1 July 2013

#3


2  

There doesn't appear to be a directive for what you want. You can either make do with the zero padding or you can change the last line to print d.strftime("%d %B %Y").lstrip('0').

似乎并没有指示你想要什么。您可以使用零填充,也可以将最后一行改为打印d。strftime(% d % B % Y).lstrip(“0”)。

#1


5  

%e is not a format code guaranteed to be processable by Python's datetime.strftime. Consult the table here for the list of format codes Python is guaranteed to support. However, since CPython's strftime calls the underlying C library's strftime, on some OS's, such as Linux, the %e format code exists.

%e不是保证可以通过Python的datetime.strftime处理的格式代码。请参阅这里的表格,了解Python所支持的格式代码列表。但是,由于CPython的strftime调用了底层C库的strftime,在某些操作系统(如Linux)上,%e格式代码是存在的。


The CPython source code says,

CPython的源代码说,

Other codes may be available on your platform. See documentation for\n\ the C library strftime function.\n"

其他代码可以在您的平台上使用。请参阅有关C库strftime函数的文档。


There is no other format code (guaranteed to exist) which behaves like %e. So if your system does not have %e or you want your code to be cross-platform compatible, use string formatting, as shown by Zacrath or Jon Clements.

没有其他格式代码(保证存在)的行为与%e类似。因此,如果您的系统没有%e,或者希望代码能够跨平台兼容,请使用字符串格式,如Zacrath或Jon Clements所示。

#2


4  

You can intermix string formatting and attribute access with the strftime style using str.format, eg:

可以使用string .format将字符串格式和属性访问与strftime样式混合使用,例如:

from datetime import datetime

s = "1 July 2013"
d = datetime.strptime(s, "%d %B %Y")
 # 2013-07-01 00:00:00
out = '{0.day} {0:%B %Y}'.format(d)
# 1 July 2013

#3


2  

There doesn't appear to be a directive for what you want. You can either make do with the zero padding or you can change the last line to print d.strftime("%d %B %Y").lstrip('0').

似乎并没有指示你想要什么。您可以使用零填充,也可以将最后一行改为打印d。strftime(% d % B % Y).lstrip(“0”)。