简体   繁体   中英

Extracting all numeric data from a file into an array in python

I have a file which contains numeric as well as string/text data. I want all the numeric data to be stored in an array (ignoring non-numeric data). If the file was purely numeric I could do this using numpy.loadtxt. However, this is not the case. An example file is shown here

BEGIN FILE

SECTION1-TEXTINFO
 ------------------------------------------------------
           2.768000     0.000001     0.000001
           0.000001     2.644491    -0.000018
           0.000001    -0.000018     2.572420
 ------------------------------------------------------
SECTION2
 ------------------------------------------------------
           2.768000     0.000001     0.000001
           0.000001     2.644491    -0.000018
           0.000001    -0.000018     2.572420
 ------------------------------------------------------
 SECTION3
 ------------------------------------------------------
           0.000343    -0.000000    -0.000000
          -0.000000     0.039522    -0.000000
          -0.000000    -0.000000     0.029825
 ------------------------------------------------------
END FILE

So at the end of the day, I want to store all numeric data in a 9*3 array.

Thank you very much for your help in advance

import numpy as np


def isFloat(element):
    try:
        float(element)
        return True
    except ValueError:
        return False

with open('data.txt', 'r') as infile:
    matrix_numpy = []
    matrix = []
    for line in infile:
        str = " ".join(line.split())
        data = [float(s) for s in str.split() if isFloat(s)]
        if data:
            matrix.append(data)
    for i in range(int(len(matrix)/3)):
        matrix_numpy.append(np.asarray(matrix[3*i:3*i+3]))

    print(matrix_numpy)

Input:

BEGIN FILE

SECTION1-TEXTINFO
 ------------------------------------------------------
           2.768000     0.000001     0.000001
           0.000001     2.644491    -0.000018
           0.000001    -0.000018     2.572420
 ------------------------------------------------------
SECTION2
 ------------------------------------------------------
           2.768000     0.000001     0.000001
           0.000001     2.644491    -0.000018
           0.000001    -0.000018     2.572420
 ------------------------------------------------------
 SECTION3
 ------------------------------------------------------
           0.000343    -0.000000    -0.000000
          -0.000000     0.039522    -0.000000
          -0.000000    -0.000000     0.029825
 ------------------------------------------------------
END FILE

Output:

[array([[  2.76800000e+00,   1.00000000e-06,   1.00000000e-06],
       [  1.00000000e-06,   2.64449100e+00,  -1.80000000e-05],
       [  1.00000000e-06,  -1.80000000e-05,   2.57242000e+00]]), 
 array([[  2.76800000e+00,   1.00000000e-06,   1.00000000e-06],
       [  1.00000000e-06,   2.64449100e+00,  -1.80000000e-05],
       [  1.00000000e-06,  -1.80000000e-05,   2.57242000e+00]]),    
 array([[ 0.000343, -0.      , -0.      ],
       [-0.      ,  0.039522, -0.      ],
       [-0.      , -0.      ,  0.029825]])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM