1

Below is my code but I'm getting ValueError. 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:

Contents of CSV file:

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
wovano
  • 4,543
  • 5
  • 22
  • 49
jaypeeee10
  • 21
  • 3

2 Answers2

3

By default, the csv module expects the file to be a proper .csv file. CSV stands for Comma Separated Values, so the values in the file should be separated by commas, like

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

The error suggests that your data is in fact separated with semicolons instead, like:

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

Since there are no commas, the entire line is treated as a single column.

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

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()

As an aside, it is better to open the file using a with block:

import csv

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

    for i in raw_1:
        for j in i:
            t_ijklm.append(int(j))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Tasty213
  • 395
  • 2
  • 10
2

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!

Almos
  • 129
  • 7