我正在尝试制作一个工资计算器,其中用户插入一个.txt文件,程序计算工作小时数
到目前为止,我能把名字、工资值和工时分开,但我不知道如何把工时加在一起
所以我想要的结果是:
员工姓名 工资(他们挣多少钱) 每个员工增加的小时数
这是数据集(txt的文件名是->;emp.txt):
Spencer 12.75 8 8 8 8 10
Ruiz 18 8 8 9.5 8 8
Weiss 14.80 7 5 8 8 10
Choi 15 4 7 5 3.3 2.2
Miller 18 6.5 9 1 4 1
Barnes 15 7.5 9 4 0 2
预期结果:
'Spencer', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes'
'12.75', '18', '14.80', '15', '18', '15'
'42', '41.5', ... and so on
当前代码:
infile = open("empwages.txt","r")
masterList = infile.readlines()
nameList = []
hourList = []
plushourList = []
for master in masterList:
nameList.append(master.split()[0])
hourList.append(master.split()[1])
x = 2
while x <= 6:
plushourList.append(master.split()[x])
x += 1
print(nameList)
print(hourList)
print(plushourList)
尝试使用
zip
:这将:
names
,第二个放在hours
,其余的放在几个小时内您可以根据需要在
*_
之前添加更多变量(编辑为正确计算小时数)
熟悉unpacking a list in Python的概念是很有用的。您可以使用以下代码来解决问题:
如果您需要您要求的字符串:
输出
如果你不反对使用
pandas
:然后您可以快速对列求和,如下所示:
相关问题 更多 >
编程相关推荐