3

I have a Python list of Numpy arrays storing X, Y, Z coordinates - like this:

[array([-0.22424938,  0.16117005, -0.39249256])
array([-0.22424938,  0.16050598, -0.39249256])
array([-0.22424938,  0.1598419 , -0.39249256]) ...,
array([ 0.09214371, -0.26184322, -0.39249256])
array([ 0.09214371, -0.26250729, -0.39249256])
array([ 0.09214371, -0.26317136, -0.39249256])]

And I would like to get them into a CSV file so I can plot them in GIS software. I am new to Numpy arrays and I keep getting errors using methods like numpy.ndarray.tofile().

I can iterate the list using

for item in list:
    f.write(str(item))

but it writes the data to the text file as binary data.
I just want to have each XYZ value comma separated with each row storing one XYZ value. Any help is appreciated.

2 Answers 2

5

Use the csv module with its writerows method:

import csv

with open('my_data.txt', 'w') as f:
    csvwriter = csv.writer(f)
    csvwriter.writerows(list_of_arrays)
Sign up to request clarification or add additional context in comments.

Comments

3

np.savetxt will write the list:

In [553]: data=[array([-0.22424938,  0.16117005, -0.39249256]),
     ...: array([-0.22424938,  0.16050598, -0.39249256]),
     ...: array([-0.22424938,  0.1598419 , -0.39249256]),
     ...: array([ 0.09214371, -0.26184322, -0.39249256]),
     ...: array([ 0.09214371, -0.26250729, -0.39249256]),
     ...: array([ 0.09214371, -0.26317136, -0.39249256]),]
In [554]: np.savetxt('test.txt',data, delimiter=', ', fmt='%12.8f')
In [555]: cat test.txt
 -0.22424938,   0.16117005,  -0.39249256
 -0.22424938,   0.16050598,  -0.39249256
 -0.22424938,   0.15984190,  -0.39249256
  0.09214371,  -0.26184322,  -0.39249256
  0.09214371,  -0.26250729,  -0.39249256
  0.09214371,  -0.26317136,  -0.39249256

np.savetxt really saves an array, but converts the list to array if needed:

In [556]: np.array(data)
Out[556]: 
array([[-0.22424938,  0.16117005, -0.39249256],
       [-0.22424938,  0.16050598, -0.39249256],
       [-0.22424938,  0.1598419 , -0.39249256],
       [ 0.09214371, -0.26184322, -0.39249256],
       [ 0.09214371, -0.26250729, -0.39249256],
       [ 0.09214371, -0.26317136, -0.39249256]])

It then iterates over rows, and writes

f.write(fmt % tuple(row))

where fmt is either the full string you provide or one constructed by replicating the shorter fmt I provided.

Effectively savetxt is doing:

In [558]: fmt='%12.8f, %12.8f, %12.8f'
In [559]: for row in data:
     ...:     print(fmt%tuple(row))
     ...:     
 -0.22424938,   0.16117005,  -0.39249256
 -0.22424938,   0.16050598,  -0.39249256
 -0.22424938,   0.15984190,  -0.39249256
  0.09214371,  -0.26184322,  -0.39249256
  0.09214371,  -0.26250729,  -0.39249256
  0.09214371,  -0.26317136,  -0.39249256

2 Comments

Hey hpaulj, how do you use cat in pyhton?
@ApolloniaVitelli, it's an alias defined for ipython on a linux system. On windows, the defined aliases are different. Try %alias to see what's defined on your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.