1

I have a CSV file with multiple rows and columns. I want to read the values into a two dimensional array so I can access each single value. There is a thread about how to read CSV files into an array which is this

When I try that code it still reads each row as one item into a list, which then I can't separate. Could you help with that?

The code I tried is this:

with open("filename.csv", newline='') as csvfile:
    data = list(csv.reader(csvfile))
    print(data)

The csv file's format is like this:

value1,value2,value3,value4

value5,value6,value7,value8

And the array that I get should be:

[[value1, value2, value3, value4]

[value5, value6, value7, value8]]

So when I say print(data[0,0]) it will print value1

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Toykan Ozdeger
  • 139
  • 2
  • 3
  • 12

1 Answers1

2

Try this i hope it will help

 datafile = open('filename.csv', 'r')
 datareader = csv.reader(datafile, delimiter=';')
 data = []
 for row in datareader:
     data.append(row)    
 print (data[1:4])
Swift
  • 790
  • 1
  • 5
  • 19