Skip to content
Advertisement

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’])

ValueError: invalid literal for int() with base 10: ‘SECRETARY a. Read the file into a list of lists (14 rows, 5 columns)

b. Transform each row of the list into a dictionary. The keys are : ename, job, salary, comm, dno. Call the resulting list of dictionaries dict_of_emp

c. Display the table dict_of_emp, one row per line

d. Perform the following computations on dict_of_emp:

D1. Compute and print the incomes of Richard and Mary (add salary and comm)

D2 Compute and display the sum of salaries paid to each type of job (i.e. salary paid to analysts is 3500 + 3500= 7000)

D3. Add 5000 to the salaries of employees in department 30. Display the new table

    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: enter image description here

Advertisement

Answer

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 i.e. with the position of the person (SECRETARY, SALESPERSON). If you then try to convert this string to an integer, you get the error.

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)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement