I am looking at the numpy.savetxt
, and am stuck at the fmt
option.
我在看麻木。savetxt,并被困在fmt选项。
I tried looking at here and also the reference in the link below all the letters that can be used for the fmt
option sort give me a general sense of what is going on.
我试着看一下这里,还有链接里的参考资料,所有可以用于fmt选项排序的字母都能让我大致了解发生了什么。
What I do not understand is if the %
symbol is required and in an example given here how 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(同样,我可能不知道如何设置浮点数…)。
2 个解决方案
#1
31
You can use the fmt
parameter in many ways, here are some examples.
您可以在许多方面使用fmt参数,这里有一些示例。
import numpy as np
a = np.array([[11,12,13,14],
[21,22,23,24]])
1) Setting floating point precision: np.savetxt('tmp.txt',a, fmt='%1.3f')
1)设置浮点精度:np.savetxt('tmp)。txt”,fmt = ' % 1.3 f ')
11.000 12.000 13.000 14.000
21.000 22.000 23.000 24.000
2) Adding characters to right-justify.
2)向右对齐添加字符。
With spaces: np.savetxt('tmp.txt',a, fmt='% 4d')
与空间:np.savetxt(tmp。txt”,fmt = ' % 4 d ')
11 12 13 14
21 22 23 24
With zeros: np.savetxt('tmp.txt',a, fmt='%04d')
0:np.savetxt(tmp。txt”,fmt = ' % 04 d ')
0011 0012 0013 0014
0021 0022 0023 0024
3) Adding characters to left-justify (use of "-
").
3)左对齐加字符(使用“-”)。
With spaces: np.savetxt('tmp.txt',a, fmt='%-4d')
与空间:np.savetxt(tmp。txt”,fmt = ' % 4 d ')
11 12 13 14
21 22 23 24
You can also use to write expressions, like (note that the number of entries in the expression is the same as the number of columns in the array:
您还可以使用它来编写表达式,比如(注意表达式中的条目数与数组中的列数相同:
4) General example: np.savetxt('tmp.txt',a, fmt='%1.1f + %1.1f / (%1.1f * %1.1f)')
4)通用的例子:np.savetxt(tmp。txt',a, fmt='%1.1f + %1.1f / (%1.1f * %1.1f)'
11.0 + 12.0 / (13.0 * 14.0)
21.0 + 22.0 / (23.0 * 24.0)
#2
1
This link might 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 '='.
当没有给出显式对齐时,在宽度字段前面加上一个0('0')字符,可以为数字类型启用标记感知的零填充。这等价于'0'的填充字符,其对齐类型为'='。
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.
精度是一个十进制数,指示在以'f'和'f'格式的浮点值的小数点后显示多少位,或者在以'g'或'g'格式的浮点值的小数点前后显示多少位。对于非数字类型,字段指示最大字段大小——换句话说,将从字段内容中使用多少字符。整数值不允许使用精度。
#1
31
You can use the fmt
parameter in many ways, here are some examples.
您可以在许多方面使用fmt参数,这里有一些示例。
import numpy as np
a = np.array([[11,12,13,14],
[21,22,23,24]])
1) Setting floating point precision: np.savetxt('tmp.txt',a, fmt='%1.3f')
1)设置浮点精度:np.savetxt('tmp)。txt”,fmt = ' % 1.3 f ')
11.000 12.000 13.000 14.000
21.000 22.000 23.000 24.000
2) Adding characters to right-justify.
2)向右对齐添加字符。
With spaces: np.savetxt('tmp.txt',a, fmt='% 4d')
与空间:np.savetxt(tmp。txt”,fmt = ' % 4 d ')
11 12 13 14
21 22 23 24
With zeros: np.savetxt('tmp.txt',a, fmt='%04d')
0:np.savetxt(tmp。txt”,fmt = ' % 04 d ')
0011 0012 0013 0014
0021 0022 0023 0024
3) Adding characters to left-justify (use of "-
").
3)左对齐加字符(使用“-”)。
With spaces: np.savetxt('tmp.txt',a, fmt='%-4d')
与空间:np.savetxt(tmp。txt”,fmt = ' % 4 d ')
11 12 13 14
21 22 23 24
You can also use to write expressions, like (note that the number of entries in the expression is the same as the number of columns in the array:
您还可以使用它来编写表达式,比如(注意表达式中的条目数与数组中的列数相同:
4) General example: np.savetxt('tmp.txt',a, fmt='%1.1f + %1.1f / (%1.1f * %1.1f)')
4)通用的例子:np.savetxt(tmp。txt',a, fmt='%1.1f + %1.1f / (%1.1f * %1.1f)'
11.0 + 12.0 / (13.0 * 14.0)
21.0 + 22.0 / (23.0 * 24.0)
#2
1
This link might 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 '='.
当没有给出显式对齐时,在宽度字段前面加上一个0('0')字符,可以为数字类型启用标记感知的零填充。这等价于'0'的填充字符,其对齐类型为'='。
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.
精度是一个十进制数,指示在以'f'和'f'格式的浮点值的小数点后显示多少位,或者在以'g'或'g'格式的浮点值的小数点前后显示多少位。对于非数字类型,字段指示最大字段大小——换句话说,将从字段内容中使用多少字符。整数值不允许使用精度。