2

我是python的初学者,很容易陷入困境和困惑......

当我读取一个包含带有数字的表格的文件时,它会将其读取为 numpy.ndarray

Python 正在改变数字的显示。例如:在输入文件中我有这个数字:56143.0254154,在输出文件中,数字写成:5.61430254e+04

但我想在输出文件中保留第一种格式。我尝试使用 string.format 或 locale.format 函数,但它不起作用

任何人都可以帮我做到这一点吗?

谢谢!鲁西

4

3 回答 3

2

Try numpy.set_printoptions() -- there you can e.g. specify the number of digits that are printed and suppress the scientific notation. For example, numpy.set_printoptions(precision=8,suppress=True) will print 8 digits and no "...e+xx".

于 2013-02-06T16:43:39.453 回答
1

如果要打印 numpy 数组,可以使用该set_printoptions函数控制不同数据类型的格式。例如:

In [39]: a = array([56143.0254154, 1.234, 0.012345])

In [40]: print(a)
[  5.61430254e+04   1.23400000e+00   1.23450000e-02]

In [41]: set_printoptions(formatter=dict(float=lambda t: "%14.7f" % t))

In [42]: print(a)
[ 56143.0254154      1.2340000      0.0123450]
于 2013-02-06T16:44:17.550 回答
0

You must create a formatted string. Assuming variable number contains the number you want to print, in Python 2.7 or 3, you could use

print("{:20.7f}".format(number))

whereas in Python 2:

print "%20.7f" % number

Alternatively, if you use Numpy routines to write out the array, use the method suggested by Warren.

于 2013-02-06T16:44:07.613 回答