我使用字符串模板从MySQL数据库中打印cur.fetchall()
数据。你知道吗
典型的元组如下所示:
(('DL2C1683', 'in', datetime.datetime(2013, 4, 28, 15, 53, 27), 'hyundai i10', '11'), ('UP1S1783', 'in', datetime.datetime(2013, 4, 28, 15, 53, 57), 'honda kinetic', '11'))
我使用以下代码打印这个元组:
template = "{0:15}|{1:15}|{2:15}|{3:15}|{4:15}"
print template.format("Registration No.", "State", "Time", "Make", "Sector") # header
for row in rows:
print template.format(*row)
datetime对象没有打印,取而代之的是15,同样在标题之后,下面的行没有正确对齐。你知道吗
输出:
Registration No.|State |Time |Make |Sector
DL2C1683 |in |15|hyundai i10 |11
UP1S1783 |in |15|honda kinetic |11
使用一些较大的宽度,例如
25
,但也添加!s
作为.format
转换标志,以强制datetime
转换为str
相关问题 更多 >
编程相关推荐