【问题标题】:ValueError error in Python code when reading from CSV file从 CSV 文件读取时 Python 代码中的 ValueError 错误
【发布时间】:2021-11-22 21:01:17
【问题描述】:

您好,我应该按照以下步骤操作。我已完成但收到此错误

文件“C:/Users/User/Desktop/question2.py”,第 37 行,在 jobtype_salary[li['job']] = int(li['salary'])

ValueError: int() 以 10 为底的无效文字:'SECRETARY 一个。将文件读入列表列表(14 行,5 列)

b.将列表的每一行转换为字典。键是:ename、job、salary、comm、dno。调用字典结果列表 dict_of_emp

c。显示表dict_of_emp,每行一行

d。对dict_of_emp进行如下计算:

D1。计算并打印 Richard 和 Mary 的收入(加上薪水和通讯)

D2 计算并显示支付给每种工作类型的工资总和(即支付给分析师的工资为 3500 + 3500= 7000)

D3。将部门 30 员工的工资加 5000。显示新表格

    import csv
    #Open the file in read mode
    f = open("employeeData.csv",'r')
    reader = csv.reader(f)
    #To read the file into list of lists we use list() method
    emps = list(reader)
    #print(emps)
    #Transform each row into a dictionary.
    dict_of_emp = [] #list of dictionaries
    for row in emps:
        d={}
        d['ename'] = row[0]
        d['job'] = row[1]
        d['salary']=row[2]
        d['comm']=row[3]
        d['dno']=row[4]
        dict_of_emp.append(d)
    print("*************************************************")
    #display the table dict_of_emp, one row per line.
    for li in dict_of_emp:
        print(li)
    print("*************************************************")
    #Incomes of Richard and Mary, to add salary and commision, first we need to cast them to integers.
    d1 = ['RICHARD','MARY']
    for li in dict_of_emp:
        if li['ename'] in d1:
            print('income of ', li['ename']," is ",int(li['salary']+li['comm']))
            
    print("*************************************************")
    #Sum of salaries based on type of job, dictionary is used so the job type is key 
    #and sum of salary is value
    jobtype_salary = {}
    for li in dict_of_emp:
        if li['job'] in jobtype_salary.keys():
            jobtype_salary[li['job']] += int(li['salary'])
        else:
            jobtype_salary[li['job']] = int(li['salary'])
    print(jobtype_salary)
    print("*************************************************")
    #Add 5000 to salaries of employees in department 30.
    for li in dict_of_emp:
        if li['dno']=='30':
            li['salary']=int(li['salary'])+5000
    for li in dict_of_emp:
        print(li)

这是 csv 作为图像:

【问题讨论】:

    标签: python


    【解决方案1】:

    我认为您的列的索引略有偏差。你做d['salary'] = row[2],根据CSV,它对应于第三行,即人的位置(秘书,销售员)。如果您随后尝试将此字符串转换为整数,则会收到错误消息。

    它会用这个来运行吗?

    for row in emps:
            d={}
            d['ename'] = row[1]
            d['job'] = row[2]
            d['salary']=row[3]
            d['comm']=row[4]
            d['dno']=row[5]
            dict_of_emp.append(d)
    

    【讨论】:

    • 我试过这个。我在 print('income of ', li['ename']," is ",int(li[' Salary']+li['comm'])) ValueError: int() 以 10 为底的无效文字:'869820-FEB-1991'
    • @Icdumort,你说得对,问题在于索引。
    • @Icdumort 你知道为什么行 print('income of ', li['ename']," 是 ",int(li['salary']+li['comm' ])) 连接而不是做加法
    • 您能否举例说明您的输出以及您的预期?乍一看似乎是因为您添加了 li['salary'] 和 li['comm'],在这个阶段它们可能都是字符串。如果您先将它们都转换为整数或浮点数,它应该可以工作。 int(li['salary']) + int(li['comm']) 而不是 int(li['salary'] + li['comm'])
    • @Icdumort,效果很好
    猜你喜欢
    • 2019-04-29
    • 2010-09-30
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2020-01-29
    • 2020-11-28
    相关资源
    最近更新 更多