Python的sqlite3:如何更改fetchall()显示元组的方式?

时间:2016-12-06 00:31:20

标签: python sqlite tuples fetchall

我正在尝试展示

ID|Class|Start|End|Days|Credit Hours
-------------------------------------
[(1, 'MATH165', '10:30', '11:45', 'M/W/F', '4'), (3, 'MATH165', '10:30', '10:45', 'M/W/F', '4'), (4, 'CSCI230', '4:30', '5:45', 'Tu/Th', '3')]

喜欢这个

ID|Class|Start|End|Days|Credit Hours
-------------------------------------
[(1, 'MATH165', '10:30', '11:45', 'M/W/F', '4'),
(2, 'W131', '4:30', '5:45', 'M/W', '3'),
(3, 'CSCI230', '4:30', '5:45', 'Tu/Th', '3')]

我的问题:是否可以更改显示的格式?

1 个答案:

答案 0 :(得分:0)

单独打印每一行

for row in c.fetchall(): 
    print(row)

或使用str()创建字符串,并将),替换为),\n

print( str(c.fetchall()).replace("),", "),\n") )

使用第一种方法,你可以做更多 - 你可以使用字符串格式,它看起来更好。

for row in c.fetchall(): 
    print("{}|{}|{}|{}|{}|{}".format(*row))

查看更多:PyFormat.info

相关问题