3

I'm trying to iterate over each cell in a CSV file, without any success. To simplify things, let say my csv file is a 3x3 matrix:

8.046875    10.4375     -0.625
0.171875    4.546875    1.953125
-4.890625   -3.703125   6.359375

Now, I'm iterating the cells with the following code:

import csv


class GetData:

    def __init__(self, path):
        self.path = path

    def read_matrix(self):
        with open(self.path, 'r') as matrix:
            csv_reader = csv.reader(matrix, delimiter=',')
            for cell in csv_reader:
                cell = ', '.join(cell)
                print(cell)


test = GetData('D:/testFile_0001.ascii.csv')
test.read_matrix()

If I run this code, it prints the matrix that shown above. When I'm changing to:

print(cell[0])

the output is:

8
0
-

My questions are: 1. why does it prints the first digit from the first column? 2. How can I print a specific cell from this matrix?

Thank you!

Pinid
  • 39
  • 1
  • 2
  • 4

1 Answers1

0

You have a few problems:

  1. Your data is not delineated by commas yet your code says that it should be
  2. csv.reader returns an array not a cell
  3. The reason why cell[0] returns one character is because you converted the array cell to a string when you did cell = ', '.join(cell). 'string'[0] will return the first character of a string that's why cell[0] only gave you the the first digit.
  4. I added read_matrix_2 which will return the matrix that you are looking for.

Here is a working example of how to access the cells: https://paiza.io/projects/gkwMf22jbQEb3CO9d1BHjQ?language=python

import csv


class GetData:

    def __init__(self, path):
        self.path = path

    def read_matrix(self):
        with open(self.path, 'r') as matrix:
            csv_reader = csv.reader(matrix, delimiter=',')
            for row in csv_reader:
                print row
                print ', '.join(row)
                    for i, cell in enumerate(row):
                        print "cell[{i}]={cell}".format(**locals())                    

    def read_matrix_2(self):
        with open(self.path, 'r') as matrix:
            csv_reader = csv.reader(matrix, delimiter=',')
            return [ row for row in csv_reader]




test = GetData('data.csv')
test.read_matrix()

print "This will give you a matrix"
matrix = test.read_matrix_2()
print matrix
print matrix[2][1]

data.csv

8.046875,10.4375,-0.625
0.171875 ,4.546875,1.953125
-4.890625 ,-3.703125,6.359375

Output

['8.046875', '10.4375', '-0.625']
8.046875, 10.4375, -0.625
cell[0]=8.046875
cell[1]=10.4375
cell[2]=-0.625
['0.171875 ', '4.546875', '1.953125']
0.171875 , 4.546875, 1.953125
cell[0]=0.171875 
cell[1]=4.546875
cell[2]=1.953125
['-4.890625 ', '-3.703125', '6.359375']
-4.890625 , -3.703125, 6.359375
cell[0]=-4.890625 
cell[1]=-3.703125
cell[2]=6.359375
This will give you a matrix
[['8.046875', '10.4375', '-0.625'], ['0.171875 ', '4.546875', '1.953125'], ['-4.890625 ', '-3.703125', '6.359375']]
-3.703125
shrewmouse
  • 5,338
  • 3
  • 38
  • 43