Search code examples
pythoncsvfor-looppreprocessorenumerate

cannot display the entire contents of the csv file


I have a problem that I can't display all the data in the csv file, the data that is output from the kede below only shows 1 line in my file, is there a solution for this

with open('test.csv','r') as f:
reader = csv.reader(f, delimiter=';')
for f, row in enumerate(reader):
  isi = row[0].split(' ')[0]
print(isi)

Solution

  • From what it looks like, you might want to try indenting the print(isi) statement at the end so that it lines up under the isi = row[].split(' ')[0] line.

    Also, I adjusted the indentation in the earlier part of your example code so that it runs on my side.

    Here is an example of what this could look like:

    import csv
    
    with open('test.csv','r') as f:
        reader = csv.reader(f, delimiter=';')
        for f, row in enumerate(reader):
            isi = row[0].split(' ')[0]
            print(isi)
    

    Using an example test.csv file like this:

    a;b;c
    d;e;f
    g;h;i
    

    The output should look like this:

    a
    d
    g