【问题标题】:why is `sum()` not working with float in `for` loop csv data?为什么 `sum()` 不适用于`for` 循环 csv 数据中的浮点数?
【发布时间】:2021-07-02 12:47:54
【问题描述】:

我只能使用一个循环,一个if 语句,并且我使用我的唯一 if 语句来过滤 csv 文件中的行以打印具有相同作业名称的特定行。所以我用tryexcept 来求平均工资收入。

为了在for 循环中给出值,我使用sum() 来填充total_salaries,但是因为薪​​水是浮动的,所以它不起作用。这是代码:

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"
        

【问题讨论】:

  • salary 只是一项,所以sum(salary) 只是要返回工资。你想要的是total_salaries += salary
  • 第一次很棒,我真的得到了一个价值,我爱死你了……但答案与预期的输出不同。我将编辑我的帖子并显示结果。
  • 同样的事情适用于instructor_count = len(line)。你只想要instructor_count += 1
  • 该值现在非常接近,但仍与预期值不同,我的输出为 1493.8,预期输出为 1974.5。所以我的整个方程式一直都是错的吗?
  • 我剪切并粘贴了您的确切代码,得到 1974.5。你是如何打印最后一行的?

标签: python csv typeerror


【解决方案1】:

这是我正在运行的代码,它与您的代码完全相同:

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

【讨论】:

  • 我的 csv 文件是这个,你能再运行一次吗?Adam Saleet,IT 支持,1000 Naji Shemmari,注册商,1250 Bilal Tweijari,讲师,2000 Abdallah Mazem,会计师,1000 Nizar Hachem,指导员,1800 Nawwara Sleiman,入学,900 Ameen Sinjer,QA 官员,1400 Walaa Saeed,指导员,1900 Hamid Shami,IT 支持,850 Nadine Khattar,指导员,1480
  • 我是个白痴对不起,我刚刚发现我正在接受所有数据讲师和非讲师,我将所有内容都放在 if 语句下并且它有效。非常感谢您的所有帮助。愿你有一个幸福的一天
【解决方案2】:

salary 不是元素列表。所以 sum(salary) 不会给出总和。你可以试试 total_salaries += 薪水

【讨论】:

  • 我现在已经这样做了,无论出于何种原因,它都没有任何价值。这是代码的最后更新
猜你喜欢
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2014-12-13
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多