水平显示csv文件

时间:2013-11-14 02:12:44

标签: python csv

InputfilesListInplane 是我正在读取数据的文件目录,我允许选择多个数据并从各自的文件夹中获取结果。 enter image description here

我的程序每次提取1个数据时都会提取FieldIP和SignalIP。它看起来像这样

Extracted FieldIP and SignalIP (SignalIP contains more data than FieldIP)
FieldIP = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16...]
SignalIP = [1,2,3,4,5,6,7,8,9,10,12,20,40...] 

我试过的代码,但数据只是垂直堆叠。

newfile = "C:/VSMPlots/TabulatedStatsVSM.csv"
with open(newfile, "wb") as csvfile:
    for file_path_IP in InputfilesListInplane: 
        writer = csv.writer(csvfile)
        writer.writerow(["FieldIP", "SignalIP"])
        writer.writerow([" "])
        for field, signal in zip(FieldIP, signalIP):
            writer.writerow([field,signal])
        writer.writerow("")

我的代码将以此格式显示

enter image description here

如何将其制作成这种格式

enter image description here

1 个答案:

答案 0 :(得分:0)

import numpy as np

a = np.asarray([[1,2,3], [4,5,6], [7,8,9]])
np.savetxt("foo.csv", a, delimiter=",")

上面的代码会给你:

1 2 3
4 5 6
7 8 9

基本上,从您的文件中读取所有数据,将它们排列在a

import numpy as np

dat1 = np.genfromtxt('foo1', delimiter=',')
dat2 = np.genfromtxt('foo2', delimiter=',')

a = np.hstack((dat1, dat2))

np.savetxt("foo.csv", a, delimiter=",", fmt="%d")

假设:

foo1:             foo2:
1,2               3,4
1,2               3,4
1,2               3,4

上面的代码给你:

1,2,3,4
1,2,3,4
1,2,3,4