【问题标题】:How to save integer array to a file showing numbers as integers如何将整数数组保存到将数字显示为整数的文件中
【发布时间】:2020-08-12 16:32:06
【问题描述】:

我正在尝试将数组保存到 txt 文件,我使用了以下函数:

numpy.savetxt('C:/Users/Adminstrator/Desktop/mesh/ELIST2-clean.txt',array2DClean, delimiter='\t')

文件中的数据如下所示:

1.300000000000000000e+01    2.710000000000000000e+02    2.360000000000000000e+02    7.200000000000000000e+01    2.350000000000000000e+02
2.400000000000000000e+01    2.760000000000000000e+02    2.060000000000000000e+02    1.310000000000000000e+02    1.300000000000000000e+02
3.200000000000000000e+01    2.580000000000000000e+02    2.820000000000000000e+02    2.570000000000000000e+02    5.000000000000000000e+01
3.600000000000000000e+01    2.800000000000000000e+02    5.100000000000000000e+01    5.000000000000000000e+01    1.030000000000000000e+02
3.900000000000000000e+01    2.800000000000000000e+02    2.250000000000000000e+02    1.120000000000000000e+02    1.110000000000000000e+02
4.300000000000000000e+01    2.810000000000000000e+02    1.630000000000000000e+02    2.200000000000000000e+01    1.640000000000000000e+02
4.900000000000000000e+01    2.850000000000000000e+02    1.150000000000000000e+02    1.600000000000000000e+02    1.610000000000000000e+02

如何将写入文件的数字格式化为没有 xe+y 表示法的整数?

【问题讨论】:

标签: python-3.x


【解决方案1】:

numpy.savetxt 有一个参数fmt,采用数字格式。

fmt:字符串或字符串序列,可选

单一格式 (%10.5f)、格式序列或多格式 字符串,例如‘Iteration %d – %10.5f’,在这种情况下,分隔符是 忽略。对于复数 X,fmt 的合法选项是:

单个说明符,fmt='%.4e',生成的数字格式类似于“ (%s+%sj)’ % (fmt, fmt)

一个完整的字符串,指定每个实部和虚部,例如' %.4e 3 列的 %+.4ej %.4e %+.4ej %.4e %+.4ej'

说明符列表,每列一个 - 在这种情况下,实数和 虚部必须有单独的说明符,例如['%.3e + %.3ej', '(%.15e%+.15ej)'] 2 列

执行numpy.savetxt('C:/Users/Adminstrator/Desktop/mesh/ELIST2-clean.txt',array2DClean, delimiter='\t', fmt='%.0f') 以使用格式字符串,以零小数位的定点表示法写入浮点数。

更多信息在这里:https://docs.python.org/3/library/string.html#format-specification-mini-language

【讨论】:

    猜你喜欢
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多