读入Staffs_2030.txt,找出其中本年度考核为优秀的员工,加薪20%,薪水四舍五入后统一保留整数位。然后重新写入Staffs_2031.txt中,新旧文件的编码方式均为’utf-8’
with open('Staffs_2030.txt','r',encoding='utf-8') as f:
a=f.readlines()
for i in a:
b=i.split(',')
if b[6]=='优秀':
b[5]=round(int(b[5])+0.2*int(b[5]),0)
str1=','.join(b)
with open('Staffs_2031.txt','w',encoding='utf-8') as g:
g.append(b)
else:
str1=','.join(b)
with open('Staffs_2031.txt','w',encoding='utf-8') as g:
g.append(b)
print('已根据2030信息生成2031对应文件')
Staffs_2030.txt
工号,姓名,职位,年龄,工龄,工资,考核结果
00001,曹横,总经理,40,10,30000,优秀
00002,郑武,经理,34,8,18000,及格
00007,李飞,经理,30,7,15600,优秀
00019,张白,经理,28,6,14000,及格
00035,王柳,经理,34,4,19200,优秀
01002,张小明,职员,26,3,8000,及格
01004,李小笑,职员,25,3,10800,优秀
01006,张三,职员,25,3,8400,优秀
01009,李四,职员,23,3,5000,及格
01013,王五,职员,22,3,6000,优秀
01015,郑六,职员,21,1,4000,及格
01016,宋七,职员,21,1,4800,优秀
错误提示
Traceback (most recent call last):
File "C:\Users\lzj\Desktop\p_SalaryRaising(1).py", line 13, in
g.append(b)
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'