Python Forum
How To Find an Opening and Closing String, Copying Open/Close/Contents to New File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Find an Opening and Closing String, Copying Open/Close/Contents to New File
#1
Hi All,

I want to use Python to parse some old BASIC code. Basically, I'm looking to locate each subroutine within the source file and then copy each subroutine out into a new file named after the subroutine.

I imagine it would go something like this:
1. Find a string containing sub nameofsub
2. Find the matching end sub for this subroutine
3. Create a new file with the name of the subroutine.
4. Copy the contents of the opening and closing strings and all contained text into this new file.
5. Repeat from the beginning.
Reply
#2
Still working through this, I've posted my code to github: https://github.com/davidshq/pyBASICTools

Right now it:
1. Loads the file into a list.
2. Iterates through each item in the list.
3. If it finds the string "DECLARE SUB" outputs a message to the display.
4. Continues parsing until the entire list (and thus contents of the file) have been searched.

About as far as I'm getting with it tonight...will update as I make more progress. Any suggestions are welcome :)

# Python BASIC Tools

# Empty List to Contain Lines of BASIC Code
baslist = []

# Open Source File and Print Contents
with open('/workspaces/python/pyBASICTools/CWSTRAT.BAS', 'rt') as basfile:
    # Copy each line into baslist.
    for line in basfile:
        baslist.append(line)

# Search for instances of SUB
index = 0 # current index in list
prev = 0 # previous index in list


while index < len(baslist):
    lineindex = 0 # current index in string
    lineprev = 0 # previous index in string
    str = baslist[index] # first string to search from baslines
    substr = "SUB animate" # substring we are looking for
    while lineindex < len(str): # While there are still more items in the list
        found = str.find(substr, lineindex)
        if found == -1:
            lineindex = lineindex + 1
            break
        print(" " * (found - lineprev) + "e", end='') # Print location of substring
        lineprev = found + len(substr) 
        lineindex += len(substr)
    prev = index
    index = index + 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 427 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Copy List Not Copying BAbdulBaki 3 378 Aug-19-2023, 02:03 AM
Last Post: perfringo
  How can i combine these two functions so i only open the file once? cubangt 4 572 Aug-14-2023, 05:04 PM
Last Post: snippsat
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 954 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Cannot find py credentials file standenman 5 1,207 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  Save and Close Excel File avd88 0 1,592 Feb-20-2023, 07:19 PM
Last Post: avd88
  selenium can't find a file in my desk ? SouAmego22 0 537 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  I cannot able open a file in python ? ted 5 1,374 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,083 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  an error while copying code astral_travel 9 1,024 Dec-04-2022, 04:01 PM
Last Post: astral_travel

Forum Jump:

User Panel Messages

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