3

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

  • I just tried and it's working. The problem can be the way you open the CSV file later on, what software do you do it with? – Christian Jan 15 '21 at 18:47
  • Thanks for answering! I'm using the latest version of Microsoft Excel. – Adrián Gómez Jan 16 '21 at 11:49
  • You're welcome. Can you show the content of the file `COORDINATES.CSV` if you open it with the simplest text editor, like notepad? – Christian Jan 16 '21 at 12:00
  • p.s.: please edit your question to provide the content of the `csv` file rather than adding a comment with it – Christian Jan 16 '21 at 12:06
  • Better this way? – Adrián Gómez Jan 16 '21 at 12:50
  • yes, it is better. So your problem is that there is always an empty line between coordinates. I think your data contains some empty entries. Can you give us a short example of what your `points` look like in python, for example by putting the line `print(points[:6]` directly below the line with `numpy.reshape(...)` – Christian Jan 16 '21 at 13:44
  • 2
    Okay, I'm pretty sure your problem is similar to [this solved question](https://stackoverflow.com/a/3191811/3283333), go check it out. Basically you just have to add `newline=''` as parameters to the `open(...)` function. – Christian Jan 16 '21 at 14:38
  • Ok, I'll check now – Adrián Gómez Jan 16 '21 at 14:40
  • Yes, adding the `newline=' '` works perfectly fine. Thank you very much for your help! – Adrián Gómez Jan 16 '21 at 14:44

0 Answers0