0

Im looking for the best way to convert a 2D list to csv file containing the coordinates (xyz) for every point.

The 2D-list has this format:

['586.732'], ['626.012'], ['496.778']
['597.422'], ['536.778'], ['586.056']
['597.422'], ['536.778'], ['586.056']

Using:

with open("output.csv", "w") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows([x, y, z])

will return a transposed list of objects, that cannot be plotted easily.

I would be happy if anyone could suggest a way to solve this.

Ma0
  • 15,057
  • 4
  • 35
  • 65
florida
  • 43
  • 1
  • 7
  • 2
    Have you tried `zip()`? https://docs.python.org/2/library/functions.html – ATOMP Nov 08 '17 at 14:10
  • 2
    The list you have showed is not 2D. What is your desired output of the sample input? – arshovon Nov 08 '17 at 14:12
  • 1
    Possible duplicate of [Matrix Transpose in Python](https://stackoverflow.com/questions/4937491/matrix-transpose-in-python) – ATOMP Nov 08 '17 at 14:22

1 Answers1

0

Using zip() will do what you want.

with open("output.csv", "w") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows( zip(x, y, z) )

The csv will look like

x1, y1, z1
x2, y2, z2
x3, y3, z3
...
ATOMP
  • 1,311
  • 10
  • 29