import re
text = """
0 10 200 300 -1
10 200 3000 3 -2
100 9 8 500 -3
"""
#居左格式化:语法如下
fmt = u"{:<4},\t{:<4},\t{:<4},\t{:<4},\t{:<4}"
print("居左格式化:")
for e in text.split("\n")[1:-1]:
m = re.search("\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(-?\d+)", e)
o = "{" + fmt.format(m.group(1), m.group(2), m.group(3), m.group(4), m.group(5)) + "},"
print(o)
#居中格式化:语法如下
fmt = u"{:^4},\t{:^4},\t{:^4},\t{:^4},\t{:^4}"
print("居中格式化:")
for e in text.split("\n")[1:-1]:
m = re.search("\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(-?\d+)", e)
o = "{" + fmt.format(m.group(1), m.group(2), m.group(3), m.group(4), m.group(5)) + "},"
print(o)
#居右格式化:语法如下
fmt = u"{:^4},\t{:>4},\t{:>4},\t{:>4},\t{:>4}"
print("居右格式化:")
for e in text.split("\n")[1:-1]:
m = re.search("\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(-?\d+)", e)
o = "{" + fmt.format(m.group(1), m.group(2), m.group(3), m.group(4), m.group(5)) + "},"
print(o)
#打印对比结果如下:
#格式化打印日期:格式2021-06-20 21:06:01
import time
fmt = u"{}-{}-{} {}:{}:{}"
date = time.localtime(time.time())
year = str(date.tm_year).zfill(4)
mon = str(date.tm_mon).zfill(2)
day = str(date.tm_mday).zfill(2)
hour = str(date.tm_hour).zfill(2)
min = str(date.tm_min).zfill(2)
sec = str(date.tm_sec).zfill(2)
date_time = fmt.format(year, mon, day, hour, min, sec)
print(date_time)