Traian Nîrca
Traian Nîrca

Reputation: 1

Extracting data from the columns of a text file

I'm currently working on a project and I got stuck with a problem. I must extract data from a text file for later use, but I don't really know how to begin with this one. This is how the data looks like:

                        21        22        23        24        25
                     (SGU)--V  (PIU)--V  (PIU)--V  (SGG)--V  (PIG)--V
   Eigenvalues --     0.12875   0.15169   0.15169   0.18748   0.22362
 1 1   Si 1S         -0.10815   0.00000   0.00000  -0.05058   0.00000
 2        2S         -1.54103   0.00000   0.00000   0.42252   0.00000
 3        3PX         0.00000  -0.50361   0.00000   0.00000  -0.48055
 4        3PY         0.00000   0.00000  -0.50361   0.00000   0.00000
 5        3PZ        -0.36562   0.00000   0.00000  -0.49574   0.00000
 6        4PX         0.00000   0.77410   0.00000   0.00000   1.31613
 7        4PY         0.00000   0.00000   0.77410   0.00000   0.00000
 8        4PZ         1.87724   0.00000   0.00000   1.02724   0.00000
 9        5D 0       -0.06070   0.00000   0.00000  -0.09982   0.00000
10        5D+1        0.00000   0.01079   0.00000   0.00000  -0.08697
11        5D-1        0.00000   0.00000   0.01079   0.00000   0.00000
12        5D+2        0.00000   0.00000   0.00000   0.00000   0.00000
13        5D-2        0.00000   0.00000   0.00000   0.00000   0.00000
14 2   Si 1S          0.10815   0.00000   0.00000  -0.05058   0.00000
15        2S          1.54103   0.00000   0.00000   0.42252   0.00000
16        3PX         0.00000  -0.50361   0.00000   0.00000   0.48055
17        3PY         0.00000   0.00000  -0.50361   0.00000   0.00000
18        3PZ        -0.36562   0.00000   0.00000   0.49574   0.00000
19        4PX         0.00000   0.77410   0.00000   0.00000  -1.31613
20        4PY         0.00000   0.00000   0.77410   0.00000   0.00000
21        4PZ         1.87724   0.00000   0.00000  -1.02724   0.00000
22        5D 0        0.06070   0.00000   0.00000  -0.09982   0.00000
23        5D+1        0.00000  -0.01079   0.00000   0.00000  -0.08697
24        5D-1        0.00000   0.00000  -0.01079   0.00000   0.00000
25        5D+2       -0.00000   0.00000   0.00000   0.00000   0.00000
26        5D-2        0.00000   0.00000   0.00000   0.00000   0.00000
                        26
                     (PIG)--V
   Eigenvalues --     0.22362
 1 1   Si 1S          0.00000
 2        2S          0.00000
 3        3PX         0.00000
 4        3PY        -0.48055
 5        3PZ         0.00000
 6        4PX         0.00000
 7        4PY         1.31613
 8        4PZ         0.00000
 9        5D 0        0.00000
10        5D+1        0.00000
11        5D-1       -0.08697
12        5D+2        0.00000
13        5D-2        0.00000
14 2   Si 1S          0.00000
15        2S          0.00000
16        3PX         0.00000
17        3PY         0.48055
18        3PZ         0.00000
19        4PX         0.00000
20        4PY        -1.31613
21        4PZ         0.00000
22        5D 0        0.00000
23        5D+1        0.00000
24        5D-1       -0.08697
25        5D+2        0.00000
26        5D-2        0.00000

I've got 26 columns like that and I must extract only the numbers in the columns. I'm wondering whether there is a certain function that can make it easier to extract in from the columns. After I don't really know how to store it, considering that I must be able to recall it after (matrix or list of lists?).

Could someone help me start?

Upvotes: 0

Views: 86

Answers (1)

paidhima
paidhima

Reputation: 2392

That looks like a tab-delimited set of values. You may be able to use the CSV module:

>>> import csv
>>> with open('file.txt', 'rb') as fin: data = list(csv.reader(fin, delimiter='\t'))

If it's not tab delimited, you could read in the file line by line and split on white space. Remove blank spaces and you'll have a line of data. Then just access the position(s) you need in each line.

Upvotes: 1

Related Questions