[英]Datetime string format alignment
In Python 2.7 I want to print datetime objects using string formatted template. 在Python 2.7中,我想使用字符串格式的模板打印日期时间对象。 For some reason using left/right justify doesn't print the string correctly.
由于某些原因,使用左/右对齐不能正确打印字符串。
import datetime
dt = datetime.datetime(2013, 6, 26, 9, 0)
l = [dt, dt]
template = "{0:>25} {1:>25}" # right justify
print template.format(*l) #print items in the list using template
This will result: 这将导致:
>25 >25
Instead of 代替
2013-06-26 09:00:00 2013-06-26 09:00:00
Is there some trick to making datetime objects print using string format templates? 是否有一些技巧使用字符串格式模板打印日期时间对象?
It seems to work when I force the datetime object into str() 当我强制将datetime对象强制为str()时似乎有效
print template.format(str(l[0]), str(l[1]))
but I'd rather not have to do that since I'm trying to print a list of values, some of which are not strings. 但我宁愿不必这样做,因为我正在尝试打印一个值列表,其中一些不是字符串。 The whole point of making a string template is to print the items in the list.
制作字符串模板的重点是打印列表中的项目。
Am I missing something about string formatting or does this seem like a python bug to anyone? 我是否遗漏了有关字符串格式的内容,或者这对任何人来说都像是一个python bug?
SOLUTION 解
@mgilson pointed out the solution which I missed in the documentation. @mgilson指出了我在文档中遗漏的解决方案。 link
链接
Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().
目前支持两个转换标志:'!s'在值上调用str(),'!r'调用repr()。
Some examples:
一些例子:
"Harold's a clever {0!s}" # Calls str() on the argument first
"Bring out the holy {name!r}" # Calls repr() on the argument first
The problem here is that datetime
objects have a __format__
method which is basically just an alias for datetime.strftime
. 这里的问题是
datetime
对象有一个__format__
方法,它基本上只是datetime.strftime
的别名。 When you do the formatting, the format function gets passed the string '>25'
which, as you've seen, dt.strftime('>25')
just returns '>25'
. 当您进行格式化时,格式化函数会传递字符串
'>25'
,正如您所见, dt.strftime('>25')
只返回'>25'
。
The workaround here it to specify that the field should be formatted as a string explicitly using !s
: 这里的解决方法是指定应该使用
!s
明确地将字段格式化为字符串:
import datetime
dt = datetime.datetime(2013, 6, 26, 9, 0)
l = [dt, dt]
template = "{0!s:>25} {1!s:>25} "
out = template.format(*l)
print out
(tested on both python2.6 and 2.7). (在python2.6和2.7上测试)。
datetime.datetime
has format method . datetime.datetime
有格式方法 。 You need to convert it str. 你需要转换它str。
>>> '{:%Y/%m/%d}'.format(dt)
'2013/06/26'
>>> '{:>20}'.format(dt)
'>20'
>>> '{:>20}'.format(str(dt))
' 2013-06-26 09:00:00'
>>> import datetime
>>> dt = datetime.datetime(2013, 6, 26, 9, 0)
>>> l = [dt, dt]
>>> template = "{0:>25} {1:>25}"
>>> print template.format(*l)
>25 >25
>>> print template.format(*map(str, l))
2013-06-26 09:00:00 2013-06-26 09:00:00
Try this: 试试这个:
print template.format(*map(str, l))
=> 2013-06-26 09:00:00 2013-06-26 09:00:00
It works by first converting the datetime
objects to a string, which then can be formatted with the format
method without problems. 它的工作原理是首先将
datetime
对象转换为字符串,然后可以使用format
方法格式化而没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.