Writing 2D array into .CSV file

Viewed 66

Sorry,I know this topic has been discussed several times already, but I have checked similar questions and none of them addresses my problem.

I'm trying to get several coordinates in format (x,y,z) to write in a .csv file. The coordinates are given to me in a 1D array of variable size (so I can have 4 points or 200). To rearrange the array and write it into de csv file I use the code below

  points = numpy.array(data.point_cloud, dtype=numpy.dtype('f4'))
  points = numpy.reshape(points, (int(points.shape[0]/3), 3))
        with open('COORDINATES.csv', 'w') as my_csv:
            csvWriter = csv.writer(my_csv, delimiter=';')
            csvWriter.writerows(points)

The print gives the following output

 [[-32.4 -21.5   0.1]
 [ 23.9  20.1   0.9]
 [-50.6  19.8   0.9]
 [ 14.3 -18.5   0.9]
 [-14.3  47.5   0.9]
 [-24.6 -21.5  -0.8]]

While this writes the coordinates, it also leaves an empty row between tuples, so my COORDINATES.csv looks like this:

CSV CONTENTS/EXCEL

CSV CONTENTS/NOTEPAD

Link to the CSV file

And I would like all the rows filled. I'm using ; as delimiter because is how my excel separates in cells, if I use , I get several rows but all the elements of the row are in the same cell. Any idea of what the problem is, or possible solution?

Note: my only limitation is that I'm not allowed to use panda

Edit: I leave a link to my .csv file as well as screenshots of the file when opened with Excel and Notepad. I also added an extra code line to print the first 6 elements of the points array

0 Answers
Related