Q:

I need to read a text file in python and get selected values into a separate array

I have a text file containing data as follows
Generated by trjconv : a bunch of waters t=   0.00000
3000   
1SOL     OW    1   1.5040   2.7580   0.6820
1SOL    HW1    2   1.4788   2.7853   0.7702
1SOL    HW2    3   1.4640   2.8230   0.6243
2SOL     OW    4   1.5210   0.9510   2.2050
2SOL    HW1    5   1.5960   0.9780   2.1520
2SOL    HW2    6   1.4460   0.9940   2.1640
3SOL     OW    7   2.1520   2.9020   2.5480
3SOL    HW1    8   2.1351   2.9646   2.6185

up to 3000 raw.
From this I need to grab selected values into a separate list, like follows:
  X=[1.5040, 1.5210, 2.1520,    etc]
  Y=[2.7580, 0.9510, 2.9020,    etc]
  Z=[0.6820, 2.2050, 2.5480,    etc]

Can any one help me to write this code?

A:

This is a good way I guess.
F = open('Data.gro', 'r')
A = open('XYZ.txt', 'w')
XO = []
YO = []
ZO = []
XHW1 = []
YHW1 = []
ZHW1 = []
XHW2 = []
YHW2 = []
ZHW2 = []
I = range(1, 10)
with open('Data.gro') as F:
    for line in F:
        if line.split()[0] == '3000':
            print('Frame')
            A.write('%s\n' % 'Frame')
            for R in I:
                line = next(F)
                P = line.split()
                if P[1] == 'OW':
                    x = float(P[3])
                    y = float(P[4])
                    z = float(P[5])
                    XO.append(x)
                    YO.append(y)
                    ZO.append(z)
                 else:
                    if P[1] == 'HW1':
                        x = float(P[3])

same pattern can use to extract data.

