简体   繁体   中英

Python 2D list of numbers to csv

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.

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
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM