python format( )强力格式化
程序员文章站
2024-03-15 14:23:11
...
format的简单用法
这是一个讲format 的简单链接。然后自己运用到python连接数据库的时候输出格式化。附上代码
import pymysql
conn = pymysql.connect(
host='localhost',
user='root',
password='lyy1314520',
db='test'
)
cur = conn.cursor()
sql = 'select * from teaches'
cur.execute(sql)
results = cur.fetchall()
number = cur.rowcount
for i in results:
count = 0
for j in i:
count +=1
print('{:^10}'.format(j),end='')
if count == 5:
print()
print('一共搜出了{0:<10.1f}个结果'.format(number))
cur.close()
conn.close()
输出结果