Python Forum
Extracting information from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting information from a file
#1
Hello guys,

I have a directory with a lot of text files, I need to loop through them and extract a certain section from them. The Text files are formatted in a standard way, in this way:

Quote:ABC: this is a text
 
SECTION 2: this is more text
 
ANOTHER SECTION: blah blah blah. This is another section
 
SECTION 4:
SECTION 5:
* A list
* Another list. I need this
 
YET ANOTHER SECTION: A bunch. of sentences. exist here.
 
OTHER FINDINGS: None.
 
FINAL
THIS IS NOT IMPORTANT

What I need to do, is to extract the "section 5" portion of the text. I know the split method, but splitting the file by ":", and then further splitting by "*" - doesn't quite seem right:

import glob

#list of all the text files
path = "reports/*.txt"


file_id=0

#loop through files, one at a time
for file_name in glob.glob(path):
    file_id += 1
    
    with open (file_name, 'rt') as myfile:
        current_file = myfile.read()
        
    section_list = current_file.split(':')
    for list_section in section_list:
        further_split = list_section.split('*')
        for x in further_split:
            print("An item in list :" + str(further_split))
Is there a more elegant/better way to get to what I need? What I am really after is that within the section that I care about, I want to loop through each of the subsections, which are delineated by "*" and work with those strings.

I would appreciate any help!
Reply


Messages In This Thread
Extracting information from a file - by lokhtar - Dec-06-2019, 03:39 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 05:40 PM
RE: Extracting information from a file - by lokhtar - Dec-06-2019, 06:07 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 09:13 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 09:41 PM
RE: Extracting information from a file - by lokhtar - Dec-09-2019, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 523 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,042 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting information from .xlsx files hobbyist 0 1,606 Jan-06-2021, 07:20 PM
Last Post: hobbyist
  getting information from a text file Nickd12 8 3,238 Nov-17-2020, 01:29 AM
Last Post: bowlofred
  Extracting data based on specific patterns in a text file K11 1 2,217 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Text file information retreval cel 4 2,534 Jun-04-2020, 02:21 AM
Last Post: cel
  Extracting CSV from Excel file 3_14ThonUser 5 2,748 May-11-2020, 05:37 PM
Last Post: buran
  Extracting hole-coordinates from a STEP-file Saksa 1 2,784 Jan-20-2020, 04:24 PM
Last Post: Larz60+
  Errors to get information of multiple files into a single file csv Clnprof 3 2,617 Aug-30-2019, 04:59 PM
Last Post: ThomasL
  Validating information from .csv file before executemany mzmingle 7 4,449 Apr-15-2019, 01:40 PM
Last Post: mzmingle

Forum Jump:

User Panel Messages

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