想分享一个发现,我在 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()
函数无法正确解析它 - 而是打印宽度?!
谢谢
最佳答案
你需要把它变成一个字符串
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
关于python - 无法在Python中使用.format()打印time(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739408/