Python Forum
Extract a string between 2 words from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract a string between 2 words from a text file
#1
Hi Forum,

I have a very long Code script containing SAS Proc SQL. There's a lot of excess code that I don't need and I know that the individual scripts will begin with 'proc sql' and end with 'quit;'.

I've read up and tried a few things but can't get any useful results.

Can anyone give me an idea of a Python3 script that would output a list the individual scripts using a .txt file?

Thanks Peter
Reply
#2
pseudo code
p = path to script dir
python_filelist = [x for x in p.iterdir() if x.is_file() and x.suffix == '.py']
# or if you only want file names without paths:
python_filelist = [x.name for x in p.iterdir() if x.is_file() and x.suffix == '.py']
example:
from pathlib import Path
import os


os.chdir(os.path.abspath(os.path.dirname(__file__)))
p = Path('.')

python_filelist = [x for x in p.iterdir() if x.is_file() and x.suffix == '.py'
    and not x.name == __file__]

print()
for file in python_filelist:
    print(file)
Output:
AddressCorrection.py BadgeDbModel.py BadgePaths.py common.py ConsolidateCsvFiles.py CreateDict.py ExtractBusinessListings.py MakePretty.py ParseBusinessListings.py PrettifyPage.py prog.py ShowFiles.py SplitAddresses.py TryAddressCorrection.py ziggy.py __init__.py
Reply
#3
So you have a large file with code and you want to extract only "proc sql" parts to a text file.
I would read the lines from the source file one by one and check for a line containing "proc sql". When found, toggle a boolean variable, say "do_copy" to True and as long as this variable is true, copy the lines to your target file.
And also check for lines containing "quit". In that case toggle the boolean to "False" so the copying stops.

sourcefile = "venv/file1.txt"   # Replace this with your source file.
targetfile = "venv/file2.txt"   # Replace this with your target file

do_copy = False     # Boolean: copy line to target or not

with open(sourcefile, "r") as source, open(targetfile, "w") as target:
    for sourceline in source:
        if "proc sql" in sourceline.lower():
            do_copy = True
        if do_copy:
            target.write(sourceline)
        if "quit" in sourceline.lower():
            target.write("\n")
            do_copy = False
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 177 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract text rektcol 6 390 Jun-28-2022, 08:57 AM
Last Post: Gribouillis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 459 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Extract parts of a log-file and put it in a dataframe hasiro 4 663 Apr-08-2022, 01:18 PM
Last Post: hasiro
  Editing text between two string from different lines Paqqno 1 518 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Converted Pipe Delimited text file to CSV file atomxkai 4 1,802 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Extract text based on postion and pattern guddu_12 2 888 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 1,247 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Replace String in multiple text-files [SOLVED] AlphaInc 5 2,878 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  fuzzywuzzy search string in text file marfer 9 2,020 Aug-03-2021, 02:41 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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