python loadtxt,格式为tab

2024-04-19 16:11:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个模拟文件,上面写着:

5.2000 -0.01047 -0.02721 0.823400 -0.56669 1.086e-5 2.109e-5 -1.57e-5 -3.12e-5
       0.823400 -0.56669 -0.02166 -0.01949 -2.28e-5 -2.66e-5 1.435e-5 1.875e-5
       1.086e-5 2.109e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01836 0.820753 -0.57065
       -1.57e-5 -3.12e-5 1.435e-5 1.875e-5 0.820753 -0.57065 -0.01066 -0.02402
5.2005 -0.01045 -0.02721 0.823354 -0.56676 1.086e-5 2.109e-5 -1.57e-5 -3.12e-5
       0.823354 -0.56676 -0.02167 -0.01947 -2.28e-5 -2.66e-5 1.435e-5 1.875e-5
       1.086e-5 2.109e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01833 0.820703 -0.57073
       -1.57e-5 -3.12e-5 1.435e-5 1.875e-5 0.820703 -0.57073 -0.01063 -0.02401
5.2010 -0.01043 -0.02721 0.823309 -0.56683 1.087e-5 2.108e-5 -1.57e-5 -3.12e-5
       0.823309 -0.56683 -0.02168 -0.01945 -2.28e-5 -2.66e-5 1.435e-5 1.874e-5
       1.087e-5 2.108e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01830 0.820654 -0.57080
       -1.57e-5 -3.12e-5 1.435e-5 1.874e-5 0.820654 -0.57080 -0.01061 -0.02400

我想得到一个float+一个float数组(float是'5.2000',数组后面是什么(4x8表) 但是numpy命令loadtxt没有这种奇特的结构。有解决办法吗?你知道吗


Tags: 文件命令numpy数组float结构解决办法loadtxt
2条回答

如果“表”始终是4x8,那么将数据作为1D数组读取可能会更容易,然后对其进行索引/整形以获得所需的输出:

# to get s you could do something like s = open(fname, 'r').read()
s = """
5.2000 -0.01047  -0.02721   0.8234   -0.56669   1.086e-5  2.109e-5 -1.57e-5  -3.12e-5
        0.8234   -0.56669  -0.02166  -0.01949  -2.28e-5  -2.66e-5   1.435e-5  1.875e-5
        1.086e-5  2.109e-5 -2.28e-5  -2.66e-5  -0.01878  -0.01836   0.820753 -0.57065
       -1.57e-5  -3.12e-5   1.435e-5  1.875e-5  0.820753 -0.57065  -0.01066  -0.02402
5.2005 -0.01045  -0.02721   0.823354 -0.56676   1.086e-5  2.109e-5 -1.57e-5  -3.12e-5
        0.823354 -0.56676  -0.02167  -0.01947  -2.28e-5  -2.66e-5   1.435e-5  1.875e-5
        1.086e-5  2.109e-5 -2.28e-5  -2.66e-5  -0.01878  -0.01833   0.820703 -0.57073
       -1.57e-5  -3.12e-5   1.435e-5  1.875e-5  0.820703 -0.57073  -0.01063  -0.02401
5.2010 -0.01043  -0.02721   0.823309 -0.56683   1.087e-5  2.108e-5 -1.57e-5  -3.12e-5
        0.823309 -0.56683  -0.02168  -0.01945  -2.28e-5  -2.66e-5   1.435e-5  1.874e-5
        1.087e-5  2.108e-5 -2.28e-5  -2.66e-5  -0.01878  -0.0183    0.820654 -0.57080
       -1.57e-5  -3.12e-5   1.435e-5  1.874e-5  0.820654 -0.5708   -0.01061  -0.02400
"""

# a 1D array of floats
x = np.array(s.split(), dtype=np.double)

# we can extract the first column by indexing every 33rd element, since each "section"
# contains one float in the left-hand column and 4*8 = 32 values in the "table".
first_col = x[::33]

# we can extract the values corresponding to the "tables" by constructing a boolean
# vector that is True wherever the index is not divisible by 33
tables = x[(np.arange(x.size) % 33) > 0]

# finally we can reshape these values to get an array of 4x8 tables stacked in the
# first dimension
tables = tables.reshape(-1, 4, 8)

print(repr(first_col))
# array([ 5.2   ,  5.2005,  5.201 ])

print(repr(tables[0]))
# array([[ -1.04700000e-02,  -2.72100000e-02,   8.23400000e-01,
#          -5.66690000e-01,   1.08600000e-05,   2.10900000e-05,
#          -1.57000000e-05,  -3.12000000e-05],
#        [  8.23400000e-01,  -5.66690000e-01,  -2.16600000e-02,
#          -1.94900000e-02,  -2.28000000e-05,  -2.66000000e-05,
#           1.43500000e-05,   1.87500000e-05],
#        [  1.08600000e-05,   2.10900000e-05,  -2.28000000e-05,
#          -2.66000000e-05,  -1.87800000e-02,  -1.83600000e-02,
#           8.20753000e-01,  -5.70650000e-01],
#        [ -1.57000000e-05,  -3.12000000e-05,   1.43500000e-05,
#           1.87500000e-05,   8.20753000e-01,  -5.70650000e-01,
#          -1.06600000e-02,  -2.40200000e-02]])

genfromtxt通常比loadtxt更通用

genfromtxt documentation

相关问题 更多 >