以csv格式导出表的表

时间:2014-01-09 09:06:41

标签: python export-to-csv

我有四个表,我想使用Python将它导出到一个CSV文件。

这是一个具体的例子。 假设我有四个这样的表: (也许是数组格式。我们可以假设所有四个表都具有相同的形状。)

#table1
x11, x12, x13,
x21, x22, x23,
x31, x32, x33,

#table2
y11, y12, y13,
y21, y22, y23,
y31, y32, y33,

#table3
z11, z12, z13,
z21, z22, z23,
z31, z32, z33,

#table4
w11, w12, w13,
w21, w22, w23,
w31, w32, w33,

我想以下列格式导出: (我更喜欢csv格式。)

x11, y11, , x12, y12, , x13, y13,   
z11, w11, , z12, w12, , z13, w12,
,,,,,,,,
x21, y21, , x22, y22, , x23, y23,   
z21, w21, , z22, w22, , z23, w23,
,,,,,,,,
x31, y31, , x32, y32, , x33, y33,   
z31, w31, , z32, w32, , z33, w33,

有人有个好主意吗?

1 个答案:

答案 0 :(得分:0)

类似

# gets you [(['x11','x12','x13'],['y11','y12','y13']),(['x21',...],['y21',...]),...]
xy_together = zip(table1, table2)

zw_together = zip(table3, table4)


# assuming the tables are all the same length...
for i in range(len(xy_together))
    x_row, y_row = xy_together[i]
    print( zip( x_row, y_row ) )

    z_row, w_row = zw_together[i]
    print( zip( x_row, y_row ) )

应该有用。

zip ()函数需要两个或更多个iterables,并在每个iterable中生成相应元素的元组列表。