在sale.in文件存储了员工一年中每个月的销售数据(csv格式,utf-8编码),请读取该文件,并计算每位员工的年销售综合,再按年度销售综合降序排序后写到sale.out文件(csv格式,utf-8编码)。
工号,1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月
5101,12880,9134,5299,5521,17611,7410,6578,7585,17145,7392,16280,19808
5102,6795,11227,9310,17819,16288,7093,7326,12608,15649,11195,9342,14097
5103,11778,19734,5221,19926,5515,14114,7282,15044,15851,18634,18191,6560
5104,16643,12989,6242,19867,18364,19012,12072,16292,5455,8927,15801,7097
5105,12243,10570,18985,7691,12688,17099,7395,14730,7682,12708,14190,6015
5106,14927,12658,5869,7990,17165,11031,5579,5403,17807,19776,19185,9732
5107,10373,6889,12498,10839,18681,15702,10454,8959,5946,12795,6391,14205
5108,18076,5237,16988,10745,8217,11326,17288,12679,8133,5003,11962,14843
5109,11936,5857,13456,7165,11734,16119,18231,11949,15966,14399,11281,19659
5110,5007,19474,11562,19441,17379,12101,19592,7374,11097,8847,9256,9122
工号,1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月,年度总销售量
5104,16643,12989,6242,19867,18364,19012,12072,16292,5455,8927,15801,7097,158761
5103,11778,19734,5221,19926,5515,14114,7282,15044,15851,18634,18191,6560,157850
5109,11936,5857,13456,7165,11734,16119,18231,11949,15966,14399,11281,19659,157752
5110,5007,19474,11562,19441,17379,12101,19592,7374,11097,8847,9256,9122,150252
5106,14927,12658,5869,7990,17165,11031,5579,5403,17807,19776,19185,9732,147122
5105,12243,10570,18985,7691,12688,17099,7395,14730,7682,12708,14190,6015,141996
5108,18076,5237,16988,10745,8217,11326,17288,12679,8133,5003,11962,14843,140497
5102,6795,11227,9310,17819,16288,7093,7326,12608,15649,11195,9342,14097,138749
5107,10373,6889,12498,10839,18681,15702,10454,8959,5946,12795,6391,14205,133732
5101,12880,9134,5299,5521,17611,7410,6578,7585,17145,7
fr = open('sale.in', 'r', encoding='utf-8')
fw = open('sale.out', 'w', encoding='utf-8')
lst = []
for line in fr:
line = line.replace('\n', '')
lst.append(line.split(','))
fr.close()
lst[0].append("年度总销售量")
for i in range(1, len(lst)):
total_income = 0
for j in range(1, len(lst[i])):
total_income += int(lst[i][j])
lst[i].append(str(total_income))
lst[1:] = sorted(lst[1:], key=lambda x: int(x[-1]), reverse=True)
for item in lst:
fw.write(','.join(item) + '\n')
fw.close()
fr = open('sale.in', 'r', encoding='utf-8')
:打开名为'sale.in'的文件,以只读模式读取(默认模式为文本模式),并指定编码为UTF-8。将文件对象赋值给变量fr
。fw = open('sale.out', 'w', encoding='utf-8')
:打开名为'sale.out'的文件,以写入模式写入(默认模式为文本模式),并指定编码为UTF-8。将文件对象赋值给变量fw
。lst = []
:创建一个空列表lst
,用于存储从文件中读取的数据。for line in fr:
:遍历文件对象fr
中的每一行数据,每次将一行赋值给变量line
。line = line.replace('\n', '')
:将变量line
中的换行符替换为空字符串,去除字符串末尾的换行符。lst.append(line.split(','))
:将使用逗号对变量line
进行分割后的结果作为列表添加到lst
列表中,实现将每行数据拆分成列表的效果。fr.close()
:关闭文件对象fr
,释放资源。lst[0].append("年度总销售量")
:在lst
列表的第一个子列表(即表头)中添加字符串"年度总销售量",作为新的列名。for i in range(1, len(lst)):
:遍历从索引1到lst
列表的长度之间的整数,每次将一个整数赋值给变量i
。total_income = 0
:创建一个变量total_income
并初始化为0,用于存储总销售量。for j in range(1, len(lst[i])):
:遍历从索引1到lst[i]
子列表的长度之间的整数,每次将一个整数赋值给变量j
。total_income += int(lst[i][j])
:将lst[i][j]
中的值解析为整数,累加到total_income
变量中,计算总销售量。lst[i].append(str(total_income))
:将总销售量转换为字符串,并添加到lst[i]
子列表的末尾,作为新的列数据。lst[1:] = sorted(lst[1:], key=lambda x: int(x[-1]), reverse=True)
:对lst
列表中从索引1开始的子列表进行排序,排序依据为子列表的最后一个元素(即总销售量),使用降序排序。重新赋值给lst[1:]
,实现根据总销售量对数据进行排序。for item in lst:
:遍历lst
列表中的每个元素,每次将一个子列表赋值给变量item
。fw.write(','.join(item) + '\n')
:将item
子列表中的元素使用逗号连接成一个字符串,并写入文件对象fw
中,末尾添加换行符。fw.close()
:关闭文件对象fw
,释放资源。