Python 为什么'sum()'不能在'for'循环csv数据中使用float?
我只能使用一个循环、一个Python 为什么'sum()'不能在'for'循环csv数据中使用float?,python,csv,typeerror,Python,Csv,Typeerror,我只能使用一个循环、一个if语句,并使用我的only-if语句过滤csv文件中的行,以打印具有相同作业名称的特定行。因此,我使用了一个尝试除了之外的方法来计算平均工资收入 为了在循环中给出的值,我使用sum()来填充工资总额,但由于工资是浮动的,所以不起作用。代码如下: for line in reader: name, job, salary = line salary = float(salary) salary = salary + (salary*0.1)
if
语句,并使用我的only-if语句过滤csv文件中的行,以打印具有相同作业名称的特定行。因此,我使用了一个尝试
除了
之外的方法来计算平均工资收入
为了在循环中给出的值,我使用sum()
来填充工资总额,但由于工资是浮动的,所以不起作用。代码如下:
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count = len(line)
total_salaries = sum(salary)
我试着写total_salaries=sum(float,salary),但这给了我一个类型错误
以下是获得完整图片的完整代码:
import csv
file = open('SP21-Emp1.txt', 'r')
reader = csv.reader(file)
total_salaries = 0
instructor_count = 0
print("Reading from file SP21-Emp1.txt" "\n")
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count += 1
total_salaries += salary
if job == 'Instructor':
instructor_count += 1
total_salaries += salary
first_name, last_name = name.split(' ', 1)
print(f'Name: {last_name} {first_name} \t Job: {job} \t Income: {salary}',)
try:
average_instructor_salary = total_salaries / instructor_count
except ValueError:
print('can not calculate average because there were no instructor salaries found.')
average_instructor_salary = "No instructor salary found"
my output was this:
Reading from file SP21-Emp1.txt
Name: Tweijari Bilal Job: Instructor Income: 2200.0
Name: Hachem Nizar Job: Instructor Income: 1980.0
Name: Saeed Walaa Job: Instructor Income: 2090.0
Name: Khattar Nadine Job: Instructor Income: 1628.0
The average of the instructors' salaries is: 1493.8
but if you look at the average for my output and the expected output that is coming up you will see that it is different.
Reading from file SP21-Emp1.txt
Name: Tweijari, Bilal Job: Instructor Income: $2200.0
Name: Hachem, Nizar Job: Instructor Income: $1980.0
Name: Saeed, Walaa Job: Instructor Income: $2090.0
Name: Khattar, Nadine Job: Instructor Income: $1628.0
The average of the instructors' salaries is: 1974.5
this is the latest update, i am a certified dumbass, i copyed and pasted the print line in the wrong place... so now i do get a value but it is still not the expected value
import csv
file = open('SP21-Emp1.txt', 'r')
reader = csv.reader(file)
total_salaries = 0
instructor_count = 0
print("Reading from file SP21-Emp1.txt" "\n")
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count += 1
total_salaries += salary
if job == 'Instructor':
first_name, last_name = name.split(' ', 1)
print(f'Name: {last_name} {first_name} \t Job: {job} \t Income: {salary}',)
try:
average_instructor_salary = total_salaries / instructor_count
print("\n" "The average of the instructors' salaries is:", average_instructor_salary)
except ValueError:
print('can not calculate average because there were no instructor salaries found.')
average_instructor_salary = "No instructor salary found"
工资不是一系列要素。所以sum(薪水)不会给出一个总数。您可以尝试total_salary+=salary这是我正在运行的代码,它与您的代码完全相同:
import csv
file = open('SP21-Emp1.txt', 'r')
reader = csv.reader(file)
total_salaries = 0
instructor_count = 0
print("Reading from file SP21-Emp1.txt" "\n")
for line in reader:
name, job, salary = line
salary = float(salary)
salary = salary + (salary*0.1)
instructor_count += 1
total_salaries += salary
if job == 'Instructor':
first_name, last_name = name.split(' ', 1)
print(f'Name: {last_name} {first_name} \t Job: {job} \t Income: {salary}',)
try:
average_instructor_salary = total_salaries / instructor_count
print("\n" "The average of the instructors' salaries is:", average_instructor_salary)
except ValueError:
print('can not calculate average because there were no instructor salaries found.')
average_instructor_salary = "No instructor salary found"
以下是我使用的CSV文件:
C:\tmp>cat SP21-Emp1.txt
Tweijari Bilal,Instructor,2000
Hachem Nizar,Instructor,1800
Saeed Walaa,Instructor,1900
Khattar Nadine,Instructor,1480
C:\tmp>
以下是我收到的输出:
C:\tmp>python x.py
Reading from file SP21-Emp1.txt
Name: Bilal Tweijari Job: Instructor Income: 2200.0
Name: Nizar Hachem Job: Instructor Income: 1980.0
Name: Walaa Saeed Job: Instructor Income: 2090.0
Name: Nadine Khattar Job: Instructor Income: 1628.0
The average of the instructors' salaries is: 1974.5
salary
只是一个项目,所以sum(salary)
只是返回salary。你想要的是total_salaries+=salary
。这是我第一次真正得到一个值,我非常喜欢你的时间……但答案与预期的结果不同。我将编辑我的帖子并显示结果。同样的事情也适用于讲师\u count=len(line)
。您只需要讲师计数+=1
。该值现在非常接近,但仍与预期值不同。我的输出为1493.8,预期输出为1974.5。那么我的整个方程一直都错了吗?我剪切并粘贴了你的精确代码,得到了1974.5。你是怎么打印最后一行的?我现在已经打印了,不管出于什么原因,它都没有显示任何价值。这是代码井的最后一次更新我的csv文件是这样的,你能再运行一次吗?Adam Saleet,it支持,1000 Naji Shemmari,注册官,1250 Bilal Tweijari,讲师,2000 Abdallah Mazem,会计师,1000 Nizar Hachem,讲师,1800 Nawwara Sleiman,准入,900 Ameen Sinjer,质量保证官,1400 Walaa Saeed,讲师,1900 Hamid Shami,it支持,850 Nadine Khattar,讲师,1480我是个白痴抱歉,我刚刚发现我接收了所有讲师和非讲师的数据,我把所有数据都放在if语句下,结果成功了。非常感谢你的帮助。愿你有一个幸福的日子