1.读写文件:
a.以写的方式打开文件stu_info.txt并向文件中写入(姓名-性别-年龄):每个一行
zhangsan-male-20
lisi-female-21
wangwu-male-20
with open('stu_info.txt', 'w') as f:
f.write('zhangsan-male-20\n')
f.write('lisi-female-21\n')
f.write('wangwu-male-20\n')
# 执行结果
b.以读的方式打开文件stu_info.txt,读取文件的每一行内容,并格式化输出,输出格式:居中对齐
姓名 性别 年龄
zhangsan male 20
lisi female 21
wangwu male 22
with open('stu_info.txt', 'r') as f:
lines = f.readlines()
print("{:^12s}{:^10s}{:^12s}".format("姓名", "性别", "年龄"))
for line in lines:
name, gender, age = line.strip().split('-')
print("{:^12s}{:^13s}{:^12s}".format(name, gender, age))
# 执行结果
2. 装饰器:要求定义一个装饰器(通用),可以打印出被装饰函数的运行时间