Skip to main content
 首页 » 编程设计

具有异国情调的表格式的python loadtxt

2025年04月02日8sky-heaven

我有一个模拟文件,内容如下:

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 没有得到这种奇特的结构。有解决办法吗?

请您参考如下方法:

如果“表格”始终是 4x8,那么以一维数组形式读取数据可能更容易,然后对其进行索引/整形以获得您想要的输出:

# 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]])