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

JavaScript

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?

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement