我不知道如何开始,但是我正在使用名为('emp_details')的文件中的信息,编码时任何人都可以帮助我的我会不断打开它。
def total_salary():
file=open('emp_details.txt','r')
tsalary = 0
for line in file:
line.split('\n')
item = line.split(',')
salary: List(str) = item[1:4]
tsalary = tsalary + str(salary)
print('The total salary for all employees in the system is: ',tsalary)
anykey = input("Enter any key to return main menu")
mainMenu()
我有一个名为“ emp_details”的文件,其中包含所有信息,包括每位员工的薪水。我正在尝试计算所有员工的总工资。
这是已读入的文件。
#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3
答案 0 :(得分:1)
似乎您正在尝试重塑各种轮子(即使您不知道有轮子)。如果您使用熊猫并将其作为csv导入,这似乎需要3行;
import pandas as pd
f = pd.read_csv('emp_details.txt', sep=',', names = ['EMP_NO', 'EMP_NAME', 'AGE', 'POSITION', 'SALARY', 'YRS_EMP'])
df = pd.DataFrame(f)
Total = df['SALARY'].sum()
编辑:经过一些测试,我意识到txt扩展名有必要跳过第一行,因此它变成了;
import pandas as pd
f = pd.read_csv('emp_details.txt', sep=',',skiprows = [0], names = ['EMP_NO', 'EMP_NAME', 'AGE', 'POSITION', 'SALARY', 'YRS_EMP'])
df = pd.DataFrame(f)
Total = df['SALARY'].sum()