将numpy数组写入具有特定列数的csv文件

时间:2015-06-05 11:32:02

标签: python csv numpy

我有一个numpy数组(444,445),我需要将它作为csv文件转储。人们可以通过以下方式实现这一目标:

np.savetxt('outfile.txt',array, delimiter=',',fmt="%s")

我使用fmt="%s"选项,因为在每行的末尾(数组的444元素是NaN)。

我想要完成的是写一个5列宽的csv文件,共有39,516行(即89个部分,每个部分由5列和444行组成),最后是NaN在第444行的末尾写为空元素。通过这种方式,矩阵中元素的数量是等价的:89x5x444=444x445或197,580个数据。

例如:

  1 xxxx,xxxx,xxxx,xxxx,xxxx,
  2 xxxx,xxxx,xxxx,xxxx,xxxx,
    ...
    ...
 89 xxxx,xxxx,xxxx,xxxx,
 90 xxxx,xxxx,xxxx,xxxx,xxxx,
 91 xxxx,xxxx,xxxx,xxxx,xxxx,
    ...
    ...
178 xxxx,xxxx,xxxx,xxxx,

我已添加了行号,以便在我的问题中更清楚。我不希望它在实际输出中。

这样做的有效和pythonic方法是什么?

目前,我正在努力使这个问题的答案适应我的情况:

Write to list to rows with specific number of columns

1 个答案:

答案 0 :(得分:1)

希望我很清楚你要求的是什么

# Reshape it

array_.reshpe(89,444,5)

# Change it's dtype to str so you can replace NaN by white spaces

array_.astype(str)

# Replace nan by white spaces

array_[array_ == 'nan'] = ''


# Finaly, save it SEE EDIT

修改

我认为np.savetxt不会使用超过2维的numpy数组,因此,引用this answer我们可以试试这个:

# Write the array to disk
with file('test.txt', 'w') as outfile:
    # I'm writing a header here just for the sake of readability
    # Any line starting with "#" will be ignored by numpy.loadtxt
    outfile.write('# Array shape: {0}\n'.format(array_.shape))

    # Iterating through a ndimensional array produces slices along
    # the last axis. This is equivalent to array_[i,:,:] in this case
    for data_slice in array_:

        # The formatting string indicates that I'm writing out
        # the values in left-justified columns 7 characters in width
        # with 2 decimal places.  
        np.savetxt(outfile, data_slice, fmt='%-7.2f')

        # Writing out a break to indicate different slices...
        outfile.write('# New slice\n')