Extract value and average

  • Thread starter Francesco Pietra
  • Start date
F

Francesco Pietra

I come 'naked', which is unusual and unfair. However, I find it
difficult to give a correct start. The files consist, among other
things, of a huge number of blocks of the type


NSTEP = 1000 TIME(PS) = 152.000 TEMP(K) = 298.54 PRESS = 89.4
Etot = -134965.2123 EKtot = 41282.1781 EPtot = -176247.3905
BOND = 1771.7644 ANGLE = 6893.3003 DIHED = 4660.1650
1-4 NB = 1931.6071 1-4 EEL = 7799.8343 VDWAALS = 19047.1551
EELEC = -218354.9960 EHBOND = 0.0000 RESTRAINT = 3.7793
EAMBER (non-restraint) = -176251.1698
EKCMT = 16048.2253 VIRIAL = 14755.8154 VOLUME = 669299.5681
Density = 0.9896
Ewald error estimate: 0.8252E-05



(in attachment what surely is a correct reproduction of columns)

I would like to extract values corresponding to variable DIHED (here
4660.1650) and getting also the mean value from all DIHED.

Thanks for giving a possible attack

francesco pietra
 
S

Steven D'Aprano

I come 'naked', which is unusual and unfair.
???

However, I find it
difficult to give a correct start. The files consist, among other
things, of a huge number of blocks of the type


NSTEP = 1000 TIME(PS) = 152.000 TEMP(K) = 298.54 PRESS =
89.4 Etot = -134965.2123 EKtot = 41282.1781 EPtot =
-176247.3905 BOND = 1771.7644 ANGLE = 6893.3003 DIHED
= 4660.1650 1-4 NB = 1931.6071 1-4 EEL = 7799.8343
VDWAALS = 19047.1551 EELEC = -218354.9960 EHBOND =
0.0000 RESTRAINT = 3.7793 EAMBER (non-restraint) =
-176251.1698 EKCMT = 16048.2253 VIRIAL = 14755.8154 VOLUME
= 669299.5681
Density =
0.9896
Ewald error estimate: 0.8252E-05



(in attachment what surely is a correct reproduction of columns)

I would like to extract values corresponding to variable DIHED (here
4660.1650) and getting also the mean value from all DIHED.

Thanks for giving a possible attack


Assuming no DIHED value will ever be split over two lines:


data = open(filename)
values = []
for line in data:
if line and line.strip(): # ignore blanks
words = line.strip().split()
try:
i = words.index("DIHED")
except IndexError:
continue
values.append(float(words[i+2]))
mean = sum(values)/len(values)


should do the job.
 
M

MRAB

Steven said:
I come 'naked', which is unusual and unfair.
???

However, I find it
difficult to give a correct start. The files consist, among other
things, of a huge number of blocks of the type


NSTEP = 1000 TIME(PS) = 152.000 TEMP(K) = 298.54 PRESS =
89.4 Etot = -134965.2123 EKtot = 41282.1781 EPtot =
-176247.3905 BOND = 1771.7644 ANGLE = 6893.3003 DIHED
= 4660.1650 1-4 NB = 1931.6071 1-4 EEL = 7799.8343
VDWAALS = 19047.1551 EELEC = -218354.9960 EHBOND =
0.0000 RESTRAINT = 3.7793 EAMBER (non-restraint) =
-176251.1698 EKCMT = 16048.2253 VIRIAL = 14755.8154 VOLUME
= 669299.5681
Density =
0.9896
Ewald error estimate: 0.8252E-05



(in attachment what surely is a correct reproduction of columns)

I would like to extract values corresponding to variable DIHED (here
4660.1650) and getting also the mean value from all DIHED.

Thanks for giving a possible attack


Assuming no DIHED value will ever be split over two lines:


data = open(filename)
values = []
for line in data:
if line and line.strip(): # ignore blanks
words = line.strip().split()
try:
i = words.index("DIHED")
except IndexError:
continue
values.append(float(words[i+2]))
mean = sum(values)/len(values)


should do the job.
str.index raises ValueError, not IndexError. Anyway, your code could be
shortened slightly:

data = open(filename)
values = []
for line in data:
words = line.split()
try:
i = words.index("DIHED")
values.append(float(words[i + 2]))
except ValueError:
pass
mean = sum(values) / len(values)
 
R

Rhodri James


Yeah, I had to go and scrub my brain out :)
Assuming no DIHED value will ever be split over two lines:


data = open(filename)
values = []
for line in data:
if line and line.strip(): # ignore blanks
words = line.strip().split()
try:
i = words.index("DIHED")
except IndexError:

Should be ValueError, not IndexError.
continue
values.append(float(words[i+2]))
mean = sum(values)/len(values)


should do the job.

You don't need the second strip(),
words = line.split()
should be sufficient.

(And we're back to the 'naked' bit again...)
 
G

Glauco

Francesco Pietra ha scritto:
I come 'naked', which is unusual and unfair. However, I find it
difficult to give a correct start. The files consist, among other
things, of a huge number of blocks of the type


NSTEP = 1000 TIME(PS) = 152.000 TEMP(K) = 298.54 PRESS = 89.4
Etot = -134965.2123 EKtot = 41282.1781 EPtot = -176247.3905
BOND = 1771.7644 ANGLE = 6893.3003 DIHED = 4660.1650
1-4 NB = 1931.6071 1-4 EEL = 7799.8343 VDWAALS = 19047.1551
EELEC = -218354.9960 EHBOND = 0.0000 RESTRAINT = 3.7793
EAMBER (non-restraint) = -176251.1698
EKCMT = 16048.2253 VIRIAL = 14755.8154 VOLUME = 669299.5681
Density = 0.9896
Ewald error estimate: 0.8252E-05



(in attachment what surely is a correct reproduction of columns)

I would like to extract values corresponding to variable DIHED (here
4660.1650) and getting also the mean value from all DIHED.

Thanks for giving a possible attack

francesco pietra

If this format is fixed...



vals = [ float(l.split('=')[-1].strip())
for l in file('file.txt','r')
if ' DIHED ' in l ]

mean = sum(vals) / len(vals)

print vals
print mean


Glauco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top