将大数组保存为具有不同格式的csv

时间:2014-01-14 13:03:06

标签: python numpy

我有一个numpy数组,大小为1000x1000。我想将其保存为CSV,可以使用以下方式完成:

numpy.savetxt('file.csv', array, delimiter = ',', fmt = '%d')

如何保存此数组,只有第1列为int32并且在float中休息?

1 个答案:

答案 0 :(得分:1)

fmt参数允许:

fmt : str or sequence of strs, optional
    A single format (%10.5f), a sequence of formats, or a
    multi-format string, e.g. 'Iteration %d -- %10.5f', in which
    case `delimiter` is ignored. For complex `X`, the legal options
    for `fmt` are:
        a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted
            like `' (%s+%sj)' % (fmt, fmt)`
        b) a full string specifying every real and imaginary part, e.g.
            `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns
        c) a list of specifiers, one per column - in this case, the real
            and imaginary part must have separate specifiers,
            e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns

在您的情况下,您可以指定一系列格式:

numpy.savetxt('file.csv', array, delimiter = ',', fmt = ['%d'] + ['%.4f']*999)