Python Forum
extract specific dat from txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract specific dat from txt file
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 496 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 972 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  Reading Specific Rows In a CSV File finndude 3 968 Dec-13-2022, 03:19 PM
Last Post: finndude
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,259 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,261 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract parts of a log-file and put it in a dataframe hasiro 4 6,270 Apr-08-2022, 01:18 PM
Last Post: hasiro
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,944 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extract a string between 2 words from a text file OscarBoots 2 1,864 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  [Solved] Trying to read specific lines from a file Laplace12 7 3,520 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Extract specific sentences from text file Bubly 3 3,390 May-31-2021, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020