2022年 11月 7日

python指定宽度20输出_Python 以指定宽度格式化输出

Python 以指定宽度格式化输出

2020-05-07

当对一组数据输出的时候,我们有时需要输出以指定宽度,来使数据更清晰。

1 mat = “{:20}\t{:28}\t{:32}”

print(mat.format(“占4个长度”,”占8个长度”, “占12长度”))

2 #如果需要居中输出在宽度前面加一个^

mat = “{:^20}\t{:^28}\t{:^32}”

print(mat.format(“占4个长度”,”占8个长度”, “占12长度”))

3 居左

mat = “{:<20}\t{:^28}\t{:>32}”

4、例如

#!/usr/bin/python3

import hashlib

import sys

import random

import string

import cx_Oracle

if __name__ == ‘__main__’:

if len(sys.argv) < 1 :

print(“Usage: %s cid uid” % (sys.argv[0]))

sys.exit()

id= sys.argv[1]

conn = cx_Oracle.connect(‘abc/123456@ip:1521/db’)

cursor = conn.cursor()

mydb1 = f”select id,userid,password from user where id={id} order by password”

try:

cursor.execute(mydb1)

except Exception as e:

conn.rollback()

print(‘failed’, e)

else:

conn.commit()

for id,userid,password in cursor:

mat = “{:^5} {:<20}\t{:^32}”

print(mat.format(id,userid,password))

cursor.close()

conn.close()

分类:数据库 | 标签: |

相关日志