Python 在 numpy.savetxt 中设置 fmt 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17043393/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the fmt option in numpy.savetxt
提问by leb
I am looking at the numpy.savetxt, and am stuck at the fmtoption. 
我正在查看numpy.savetxt,并被困在该fmt选项上。
I tried looking at hereand also the reference in the link below all the letters that can be used for the fmtoption sort give me a general sense of what is going on. 
我尝试查看here以及下面链接中可用于fmt选项排序的所有字母的参考,让我对正在发生的事情有一个大致的了解。
What I do not understand is if the %symbol is required and in an example given herehow should I interpret the 10.5 number? 
If "f" is about setting the floating point, then how come is it 10.5 (then again, I might not know how floating points are set...).
我不明白的是,如果%需要符号,在这里给出的示例中,我应该如何解释 10.5 数字?如果“f”是关于设置浮点数,那么它怎么会是 10.5(再说一次,我可能不知道如何设置浮点数......)。
采纳答案by Saullo G. P. Castro
Knowing that np.savetxtonly works for 1D or 2D arrays, the general idea is:
知道np.savetxt只适用于一维或二维数组,一般的想法是:
- when fmtis a single formatting string it applies to all elements in the array (1D or 2D input array)
- when fmtis a sequence of formatting strings, it applies to each column of the 2D input array
- 当fmt是单个格式化字符串时,它适用于数组中的所有元素(一维或二维输入数组)
- whenfmt是格式化字符串序列,它适用于二维输入数组的每一列
I'm presenting here some examples using the following input array:
我在这里展示了一些使用以下输入数组的示例:
import numpy as np
a = np.array([[11, 12, 13, 14],
              [21, 22, 23, 24],
              [31, 32, 33, 34]])
1) Setting floating point precision: np.savetxt('tmp.txt', a, fmt='%1.3f')
1) 设置浮点精度: np.savetxt('tmp.txt', a, fmt='%1.3f')
11.000 12.000 13.000 14.000
21.000 22.000 23.000 24.000
31.000 32.000 33.000 34.000
2) Adding characters to right-justify.
2) 添加字符以右对齐。
With spaces: np.savetxt('tmp.txt', a, fmt='% 4d')
带空格: np.savetxt('tmp.txt', a, fmt='% 4d')
  11   12   13   14
  21   22   23   24
  31   32   33   34
With zeros: np.savetxt('tmp.txt', a, fmt='%04d')
带零: np.savetxt('tmp.txt', a, fmt='%04d')
0011 0012 0013 0014
0021 0022 0023 0024
0031 0032 0033 0034
3) Adding characters to left-justify (use of "-").
3) 向左对齐添加字符(使用“ -”)。
With spaces: np.savetxt('tmp.txt', a, fmt='%-4d')
带空格: np.savetxt('tmp.txt', a, fmt='%-4d')
11   12   13   14  
21   22   23   24  
31   32   33   34  
4) When fmtis a sequence of formatting strings, each row of a 2D input array is processed according to fmt:
4) whenfmt是格式化字符串序列,二维输入数组的每一行都按照以下方式处理fmt:
fmtas a sequence in a single formatting string
fmt作为单个格式化字符串中的序列
fmt = '%1.1f + %1.1f / (%1.1f * %1.1f)'
np.savetxt('tmp.txt', a, fmt=fmt)
11.0 + 12.0 / (13.0 * 14.0)
21.0 + 22.0 / (23.0 * 24.0)
31.0 + 32.0 / (33.0 * 34.0)
fmtas an iterator of formatting strings:
fmt作为格式化字符串的迭代器:
fmt = '%d', '%1.1f', '%1.9f', '%1.9f'
np.savetxt('tmp.txt', a, fmt=fmt)
11 12.0 13.000000000 14.000000000
21 22.0 23.000000000 24.000000000
31 32.0 33.000000000 34.000000000
回答by FFT
This linkmight be helpful.
此链接可能会有所帮助。
From the link:
从链接:
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='.
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.
宽度是定义最小字段宽度的十进制整数。如果未指定,则字段宽度将由内容决定。
当没有给出明确的对齐方式时,在宽度字段前面加上一个零 ('0') 字符,为数字类型启用符号感知零填充。这等效于对齐类型为“=”的填充字符“0”。
精度是一个十进制数,表示对于用 'f' 和 'F' 格式的浮点值小数点后应显示多少位,或对于用 'g' 格式的浮点值小数点前后应显示多少位或'G'。对于非数字类型,该字段指示最大字段大小 - 换句话说,字段内容中将使用多少个字符。整数值不允许使用精度。

