简体   繁体   English

从 CSV 文件读取时 Python 代码中的 ValueError 错误

[英]ValueError error in Python code when reading from CSV file

Hello am supposed to the steps below.您好,我应该按照以下步骤操作。 I have finished but getting this error我已完成但收到此错误

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

ValueError: invalid literal for int() with base 10: 'SECRETARY a. ValueError: int() 以 10 为底的无效文字:'SECRETARY a. Read the file into a list of lists (14 rows, 5 columns)将文件读入列表列表(14 行,5 列)

b.湾。 Transform each row of the list into a dictionary.将列表的每一行转换为字典。 The keys are: ename, job, salary, comm, dno.键是:ename、job、salary、comm、dno。 Call the resulting list of dictionaries dict_of_emp调用字典的结果列表 dict_of_emp

c. c。 Display the table dict_of_emp, one row per line显示表dict_of_emp,每行一行

d. d。 Perform the following computations on dict_of_emp:对 dict_of_emp 执行以下计算:

D1. D1。 Compute and print the incomes of Richard and Mary (add salary and comm)计算并打印 Richard 和 Mary 的收入(添加薪水和通讯)

D2 Compute and display the sum of salaries paid to each type of job (ie salary paid to analysts is 3500 + 3500= 7000) D2 计算并显示支付给每种工作类型的工资总和(即支付给分析师的工资是 3500 + 3500= 7000)

D3. D3。 Add 5000 to the salaries of employees in department 30. Display the new table将部门 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)

Here is the csv as an image:这是 csv 作为图像: 在此处输入图像描述

I think the indexing of your columns is slightly off.我认为您的列的索引略有偏差。 You do d['salary'] = row[2] , which, according to the CSV corresponds with the third row ie with the position of the person (SECRETARY, SALESPERSON).你做d['salary'] = row[2] ,根据 CSV 对应于第三行,即与人(秘书,销售人员)的 position 对应。 If you then try to convert this string to an integer, you get the error.如果您随后尝试将此字符串转换为 integer,则会收到错误消息。

Does it run with this instead?它是否与此一起运行?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM