Python Forum

Full Version: extract specific dat from txt file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have alot of txt files that contains data i need to extract; for example:

    ** n zone    1 vzone=  465.99     /  1  2  3  4  5  6  7  8  9 10 11 12 13 14
*grp*flux 1/cm2c  * stotal      * sabs        * sfis.       * nu$sfis.    * 1/3*strans  *1/aver.veloci*aver power
  1  0.8340961E-01 0.5082535     0.8274327E-02 0.1694078E-02 0.4426407E-02  1.408055     0.1321336E-05 0.5349567     0.1364792    
  2  0.2819324E-01  1.088884     0.5163670E-01 0.3171108E-01 0.7712194E-01 0.3993335     0.2996155E-04  1.493549     0.8635209
the values of "sabs" are on the next two lines; how to search for "sabs" and store its value in avariable?
i can do that if the values of "sabs" were on the same line because that line has a unique set of characters i know, unlike the next 2 lines
If the number of columns is constant, just split each line on whitespace and the sabs values are the 4th column:

row = # read in the data
sabs_value = row.split(' ')[3]

# or if you don't know what whitspace characters they are...
import re  # regular expression module
sabs_value = re.split(r'\s+', row)[3]   # \s+  matches one or more whitespace characters