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

标签 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() 函数无法正确解析它 - 而是打印宽度?! 谢谢

最佳答案

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

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/

相关文章:

python - 通过字符列表列表生成单词列表

python - 无法用Python编写网络爬虫

python - Django i18n_patterns : resolve() doesnt work as expected

python - py2exe 可执行文件未正确刷新标准输出

python - 相当于 Python 中的 BASH_XTRACEFD 重定向

python - Python中使用默认值的多列表迭代

python - OpenCV摄像机校准对模拟数据失败

python - SSL错误 : ("bad handshake: SysCallError(54, ' ECONNRESET')", )

python - 将程序作为可执行文件运行时设置默认 python 版本 ./xxx.py - 在 linux 上

Python将输入与文件中的行进行比较?