Taylor Patterson Taylor Patterson - 23 days ago 6
Python Question

Why does my code produce this output (Python)?

I'm trying to read a csv file into a list. However, my code doesn't produce what it's supposed to.

data_list = []

data_file = open('table.csv', 'r')

for line in data_file:
data_list.append(line.strip().split(','))
print (data_list)


It produces this:

enter image description here

I couldn't code it on here so I had to attach a photo (sorry). Why does it do this? I just want a normal list of lists.

Answer

You're printing inside the for loop. data_list is a list of lists, just print it after the for loop finishes :)

data_list = []

data_file = open('table.csv', 'r')

for line in data_file:
    data_list.append(line.strip().split(','))
print(data_list)