Home > database >  Value Error: invalid literal for int() with base 10
Value Error: invalid literal for int() with base 10

Time:05-30

Please help, below is my code but I'm getting value error. My input is from csv file containing integers, but unable to import them:

import csv

excel_1 = open(r"C:\Users\JP Dayao\Desktop\Python files\test_file.csv")
raw_1 = csv.reader(excel_1)
t_ijklm = []   

for i in raw_1:
    for j in i:
        t_ijklm.append(int(j))
excel_1.close()   


Output (with error):

ValueError                                Traceback (most recent call last)
Input In [5], in <cell line: 17>()
     17 for i in raw_1:
     18     for j in i:
---> 19         t_ijklm.append(int(j))
     20 excel_1.close()   
     22 #import constraints data

ValueError: invalid literal for int() with base 10: 

'21;19;21;19;24;23;35;35;36;35;36;35;35;23;22;30;23;36;23;26;25;25;40;25;40;25;22;21;21;25;20;25;22;22;18;19;25;18;25;17;23;20;24;24;22;24;21;22;23;21;27;22;36;23;25;29;29;23;28;33;29;31;19;26;32;20;35

CodePudding user response:

welcome to stack overflow,

It looks to me like you're trying to use the CSV import module to import a file. The module expects the file to be a comma-separated list of values (Comma Separated Values). The error suggests that your data is in fact semicolon (;) separated not comma separated.

Semi colon separated:

21;19;21;19;24;23;35;35;36;35;36;35;35;23;22;30;23;36;23;26;25;25;40;25;40;25;22;21;21;25;20;25;22;22;18;19;25;18;25;17;23;20;24;24;22;24;21;22;23;21;27;22;36;23;25;29;29;23;28;33;29;31;19;26;32;20;3

vs comma seperated

21,19,21,19,24,23,35,35,36,35,36,35,35,23,22,30,23,36,23,26,25,25,40,25,40,25,22,21,21,25,20,25,22,22,18,19,25,18,25,17,23,20,24,24,22,24,21,22,23,21,27,22,36,23,25,29,29,23,28,33,29,31,19,26,32,20,3

you can tell the CSV module to use ; as the sepreator (delimiter) not comma. Do this by passing delimiter=";" to the csv.reader method. Full solution is:

import csv

excel_1 = open(r"test_file.csv")
raw_1 = csv.reader(excel_1, delimiter=";")
t_ijklm = []

for i in raw_1:
    for j in i:
        t_ijklm.append(int(j))
excel_1.close()

CodePudding user response:

You need to add a delimeter in the csv.reader function. for example:

with open(r"C:\Users\JP Dayao\Desktop\Python files\test_file.csv") as csvfile:
  file_reader = csv.reader(csvfile, delimiter=';')
  for row in file_reader:
    --your code--

Hope it helps!

  • Related