我需要能够以可以将格式规范传递给numpy.savetxt()中的fmt
参数的方式执行此操作。我想:
我试过了:
'{:0>8}'.format('%.3f')
我传递给numpy.savetxt()函数,但它无法将值截断为指定的8位宽度。
例如:
import numpy as np
arr = np.random.random(10,10)*10
np.savetxt("testfile.txt", arr, fmt='{:0>8}'.format('%.3f')), delimiter='')
给出输出:
0000-11.54900006.870000021.8620000-0.3420000-20.55700005.5390000-2.343000012.1160000-8.949000012.097
00002.1510000-8.29800004.4310000-6.1050000-4.2280000-11.462000019.7120000-10.29800006.848000013.074
0000-0.536000010.8160000-10.329000011.328000017.5580000-2.83200007.795000012.10400001.834000011.550
0000-2.6060000-10.25000008.5310000-6.839000010.7350000-15.7570000-0.274000011.17500000.9940000-7.971
000022.6050000-1.4860000-2.37000000.806000010.3560000-8.86100002.430000018.6110000-3.1820000-5.281
000022.0720000-7.299000018.0320000-2.556000010.4690000-1.21900006.23900006.3840000-8.1560000-10.781
0000-2.0590000-4.8680000-22.31700005.0220000-9.5820000-2.873000021.6050000-0.9110000-9.7050000-4.920
0000-8.4410000-4.061000023.6880000-2.7050000-0.5700000-3.4070000-10.48900005.1820000-2.32500000.581
000016.9450000-19.11100002.955000018.28900000.62500003.34200003.1910000-14.7660000-4.93600002.139
000016.1390000-1.58100002.095000032.02800006.63900002.07300006.3080000-12.91900001.42500001.447
显然,这些值没有被正确截断,因为每一行应该是相同的长度,并且每列应该基本上是固定宽度。
如何修改传递给numpy的savetxt函数的fmt
参数的格式说明符,以获得所需的结果?