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
  extract substring from a string before a word !! evilcode1 3 531 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Need to replace a string with a file (HTML file) tester_V 1 751 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Form that puts diacritics on the words in the text Melcu54 13 1,457 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,120 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Pulling Specifics Words/Numbers from String bigpapa 2 748 May-01-2023, 07:22 PM
Last Post: bigpapa
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 972 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,105 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 2,908 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract only certain text which are needed Calli 26 5,813 Oct-10-2022, 03:58 PM
Last Post: deanhystad
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,261 Jul-27-2022, 12:31 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