I am preparing a file in Python to be read in Fortran. The file looks like this:
我正在用Python准备一个用Fortran读取的文件。文件如下:
0.0000000E+00 0.0000000E+00 0.0000000E+00 1.5000000E-01
1.0193980E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
2.0387960E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
3.0581940E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
4.0775920E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
5.0969900E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
6.1163880E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
7.1357860E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
8.1551840E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
it starts with one leading blank space and then the spacing between columns becomes 2 blanks. The Fortran executable that I have (no access to the source code), reads and writes E15.7
format which means I need two leading blanks like the following:
它从一个前导空格开始,然后列之间的间隔变成两个空格。我拥有的Fortran可执行文件(无法访问源代码),读取和编写E15.7格式,这意味着我需要两个主要空格,如下所示:
0.0000000E+00 0.0000000E+00 0.0000000E+00 1.5000000E-01
1.0193980E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
2.0387960E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
3.0581940E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
4.0775920E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
5.0969900E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
6.1163880E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
7.1357860E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
8.1551840E-02 0.0000000E+00 0.0000000E+00 1.5000000E-01
My python script that produce the file is like this:
生成该文件的python脚本如下:
np.savetxt(fpath, data, fmt='%14.7E')
If I use fmt='%15.7E'
the leading blanks becomes two but the rest of the spaces will be 3.
如果我使用fmt='%15.7E',那么前面的空格将变成2,但其余的空格将是3。
1 个解决方案
#1
2
With a bit more involved format string you can do that like:
使用稍微复杂一点的格式字符串,您可以这样做:
Code:
np.savetxt(fpath, data, fmt=' %14.7E' * data.shape[1])
Test Code:
import numpy as np
fpath = 'test_data'
data = np.array([[1.0, 1.1, 12.0]])
np.savetxt(fpath, data, fmt=' %14.7E' * data.shape[1])
Results:
1.0000000E+00 1.1000000E+00 1.2000000E+01
#1
2
With a bit more involved format string you can do that like:
使用稍微复杂一点的格式字符串,您可以这样做:
Code:
np.savetxt(fpath, data, fmt=' %14.7E' * data.shape[1])
Test Code:
import numpy as np
fpath = 'test_data'
data = np.array([[1.0, 1.1, 12.0]])
np.savetxt(fpath, data, fmt=' %14.7E' * data.shape[1])
Results:
1.0000000E+00 1.1000000E+00 1.2000000E+01