我需要编写一个python程序,它可以读取1000行文本,计算每个教授类别(助理、助理、全职、所有教员)的工资总额和平均值。问题是:
A university posts its employee salary at http://cs.armstrong.edu/liang/data/Salary.txt. Each line in the file consists of faculty first name, last name, rank, and salary (see Exercise 13.16). Write a program to display the total salary for assistant professors, associate professors, full professors, and all faculty, respectively, and display the average salary for assistant professors, associate professors, full professors, and all faculty, respectively.
这是我目前掌握的代码:
import urllib.request
def main():
infile = urllib.request.urlopen('http://cs.armstrong.edu/liang/data/Salary.txt')
s = infile.read().decode().splitlines()
asstTotal = 0
asstCount = 0
asscTotal = 0
asscCount = 0
fullTotal = 0
fullCount = 0
for line in s:
if "assistant" in line:
asstCount += 1
asstTotal += int(float(s[3]))
if "associate" in line:
asscCount += 1
asscTotal += int(float(s[3]))
if "full" in line:
fullCount += 1
fullTotal += int(float(s[3]))
allFacTotal = asstTotal + asscTotal + fullTotal
avgAsst = int(asstTotal / asstCount)
avgAssc = int(asscTotal / asscCount)
avgFull = int(fullTotal / fullCount)
avgAll = int(allFacTotal / 1000)
print("Total assistant professor salary is", asstTotal)
print("Total associate professor salary is", asscTotal)
print("Total full professor salary is", fullTotal)
print("Total faculty salary is", allFacTotal)
print("Average assistant professor salary is", avgAsst)
print("Average associate professor salary is", avgAssc)
print("Average full professor salary is", avgFull)
print("Average faculty salary is", avgAll)
main()
我不知道如何从各自的教授级别获得薪水,以及如何合计和平均。任何意见都将不胜感激!在
结果:
^{pr2}$
通过在字典中列出每个职位的工资清单,然后简单地将总和除以长度,就可以计算出任何级别的级别。在
我的例子:
以及输出:
^{pr2}$在python2.7中,使用
urllib2.urlopen
并将salaries按标题与表一起分组。在在python3中,使用}
urllib.request.urlopen
并将print ..
改为{相关问题 更多 >
编程相关推荐