Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在savetxt中舍入numpy值?_Python_Numpy_Formatting_Rounding - Fatal编程技术网

Python 如何在savetxt中舍入numpy值?

Python 如何在savetxt中舍入numpy值?,python,numpy,formatting,rounding,Python,Numpy,Formatting,Rounding,使用savetxt保存时无法格式化numpy数组: from numpy import * a = loadtxt('frequenz.txt') b = sum(a[0:10,1:4], 1)/3-20741 savetxt('solution.txt', b, fmt='%.2f', delimiter='\t', header='average') 显示一个错误: fh.write(asbytes(format % tuple(row) + newline)) UnboundLocal

使用savetxt保存时无法格式化numpy数组:

from numpy import *

a = loadtxt('frequenz.txt')
b = sum(a[0:10,1:4], 1)/3-20741
savetxt('solution.txt', b, fmt='%.2f', delimiter='\t', header='average')
显示一个错误:

fh.write(asbytes(format % tuple(row) + newline))
UnboundLocalError: local variable 'format' referenced before assignment
在不使用fmt保存值时,它可以工作,但我只希望有2个小数:

from numpy import *

a = loadtxt('frequenz.txt')
b = sum(a[0:10,1:4], 1)/3-20741
savetxt('solution.txt', b, delimiter='\t', header='average')
solution.txt:

# average
3.666666666667879326e+00
6.666666666667879326e+00
9.333333333332120674e+00
1.300000000000000000e+01
1.533333333333212067e+01
1.833333333333212067e+01
2.133333333333212067e+01
2.466666666666787933e+01
2.766666666666787933e+01
3.066666666666787933e+01
frequenz.txt:

#0  20741   20741   20741
6   20745   20745   20744   20739   20739   20738
12  20748   20747   20748   20736   20736   20736
18  20750   20751   20750   20732   20733   20732
24  20754   20754   20754   20730   20730   20730
30  20756   20756   20757   20727   20727   20726   
36  20760   20759   20759   20723   20723   20724
42  20762   20762   20763   20721   20721   20720
48  20766   20766   20765   20718   20718   20718
54  20768   20769   20769   20715   20715   20714
60  20771   20772   20772   20712   20712   20712   
谢谢你的帮助!
Martin

我的解决方案:我总是在编辑器BBEdit中键入python代码,然后在终端中运行它。这不起作用,并给出一个错误。直接把它输入终端就可以了。python文档的默认标题是

from __future__ import (print_function, division, unicode_literals, absolute_import)
错误是由以下原因引起的:

unicode_literals

也许它能帮助别人。关于。

我的解决方案:我总是在编辑器BBEdit中键入python代码,然后在终端中运行它。这不起作用,并给出一个错误。直接把它输入终端就可以了。python文档的默认标题是

from __future__ import (print_function, division, unicode_literals, absolute_import)
错误是由以下原因引起的:

unicode_literals

也许它能帮助别人。注意。

您使用的是什么版本的numpy和python?我经常使用numpy,大多数时候我使用
np.savetxt
指定格式,我不记得有任何类似的错误。我使用numpy 1.7.1.python版本:2.7.5,numpy版本:1.7。1@WarrenWeckesser第一个代码是否也适用于您?您的解决方案文件中的数字是否四舍五入?我应该说,
savetxt
调用对我有效。我使用了
b=np.linspace(0,1,5)
。文件中的值按预期格式化。您使用的是哪个版本的numpy和python?我经常使用numpy,大多数时候我使用
np.savetxt
指定格式,我不记得有任何类似的错误。我使用numpy 1.7.1.python版本:2.7.5,numpy版本:1.7。1@WarrenWeckesser第一个代码是否也适用于您?您的解决方案文件中的数字是否四舍五入?我应该说,
savetxt
调用对我有效。我使用了
b=np.linspace(0,1,5)
。文件中的值已按预期格式化。