无法在Python中使用.format()打印time()

时间:2017-12-10 13:38:56

标签: python python-2.7

想要分享一个发现,我在Python中使用arrow()模块并希望打印时间,所以我写道:

import arrow

a = "08:26:18.788976"
n = arrow.utcnow().time()

print type(n)
# <type 'datetime.time'>


print "{first:20} {second:20}".format(first="This is the first", second=a)
print "{first:20} {second:20}".format(first="This is the first", second=n)

输出

This is the first    08:26:18.788976
This is the first    20

使用n变量时,format()函数无法正确解析它 - 而是打印宽度?! 感谢

1 个答案:

答案 0 :(得分:2)

你需要把它变成一个字符串

n = str(arrow.utcnow().time())

返回:

<class 'str'>
This is the first    08:26:18.788976     
This is the first    13:41:12.943499   

只是因为你没有将它定义为字符串,所以你知道它返回了字符数而不是时间..

u = "type 'datetime.time'"
print len(u)

输出:

 20