0

i want to import a csv file to python and convert numbers to int , each row has different number (row 1 has 4 grade and row 2 has 10 grade) when i want to convert number i got bellow error ValueError: invalid literal for int() with base 10: '' please be informed when i enter equal number in each row problem has been solved .

would you please help me.

my code:

import csv
from statistics import mean

with open ('d:\source.csv') as f :
    reader=csv.reader(f)
    for row in reader:
        name=row[0]
        these_grades=list()
        for grade in row[1:]:
            these_grades.append(int(grade))

csv file :

mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,19,10,19,6,8,14,3
sara,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8

error:

ValueError: invalid literal for int() with base 10: ''
13
  • It looks like your code lost its formatting. Please indent your code by four spaces or select it all and press Ctrl+K. Commented Mar 22, 2020 at 12:51
  • Have a look into pandas. Use this to read your CSV and convert your values to int, as well as run your mean calc. Additionally, please reformat your question so the code block appears in code format. Commented Mar 22, 2020 at 12:53
  • this element is the problem '', '' Commented Mar 22, 2020 at 13:17
  • @Pygirl--can you give the full line where '', '' occurs? I don't see it. I don't get an error in running the code and can print out these_grades in the for loop. Commented Mar 22, 2020 at 13:24
  • Add the full traceback. It contains further information that may help us help you. Commented Mar 22, 2020 at 13:25

1 Answer 1

1

When doing !cat source.csv this is the output I get:

mandana,5,7,3,15,,,,,,,,,,,
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,19,10,19,6,8,14,3,,,,,,,,
sara,0,5,20,14,,,,,,,,,,,
soheila,13,2,5,1,3,10,12,4,13,17,7,7,,,
ali,1,9,,,,,,,,,,,,,
sarvin,0,16,16,13,19,2,17,8,,,,,,,

The error you are getting because there is "" present which can't be converted into int.

like for 1st row value is :

['mandana', '5', '7', '3', '15', '', '', '', '', '', '', '', '', '', '', '']

You need to get rid of these '' by using filter:

row = list(filter(None, row))

Try:

import csv
from statistics import mean

with open ('source.csv') as f :
    reader=csv.reader(f)
    for row in reader:
        row = list(filter(None, row))
        name=row[0]
        these_grades=list()
        for grade in row[1:]:
            these_grades.append(int(grade))

these_grades:

[0, 16, 16, 13, 19, 2, 17, 8]

edit:

import csv
from statistics import mean

these_grades=list()
with open ('source.csv') as f :
    reader=csv.reader(f)
    for row in reader:
        grades = list()
        row = list(filter(None, row))
        name=row[0]
        for grade in row[1:]:
            grades.append(int(grade))
        these_grades.append(grades)

these_grades:

[[5, 7, 3, 15],
 [3, 9, 4, 20, 9, 1, 8, 16, 0, 5, 2, 4, 7, 2, 1],
 [19, 10, 19, 6, 8, 14, 3],
 [0, 5, 20, 14],
 [13, 2, 5, 1, 3, 10, 12, 4, 13, 17, 7, 7],
 [1, 9],
 [0, 16, 16, 13, 19, 2, 17, 8]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.