suffa
suffa

Reputation: 3796

How to use Python to read a range of lines in a file

I am trying to accomplish the task of reading a recurring range of lines in a file. The line start with 004 and end with 819 (these represent orders. could be 1 or 1000+ orders - recurring range). I want to get specific files that have a certain number on line 004 and get all lines up to 819, and any other matching 004 lines thereafter. I have attempted, but got stuck at getting lines in a range. I am trying to think past this, but keeping hitting a wall. Any help and/or direction is appreciated.

The file I'm reading from is a .txt file with the format of:

...
004 54566
006 P56
008 Name 
010 61758012
018 UMC  
027
...
819 Staven

Here is the code that I've written thus far to read the line:

os.chdir(r"C:\\mydirectory")
wrkdir = os.getcwd()
filelist = os.listdir(wrkdir)

sampleList = [filename for filename in filelist if filename.endswith(".SFX")]

filelist = sampleList

for sfx in filelist:
with open(sfx, 'r') as rfile:
    lines = rfile.readlines()
    for line in lines:
        count+=1
        if line[0:3] == '004':
            gstring = line.split(' ')
            #if gstring[1] == '8041': (This line did not work for me)
                print line #(ultimately I will write these lines to a new file)

I'm drawing a blank after this, not sure how to print in between line ranges 004 - 819, if 004 == 'certain number'?

Upvotes: 1

Views: 5061

Answers (3)

gkusner
gkusner

Reputation: 1244

basically a state machine

for sfx in filelist:
    with open(rxf, 'r') as rfile:
        keep = False
        for line in rfile:
            if line.startswith('004'):
                gstring = line.split() # white space
                if '8041' in gstring:
                    keep = True

            if keep and line.startswith('819'):
                print line
                keep = False

            if keep:
                print line

Upvotes: 3

Jay
Jay

Reputation: 1599

This should work. Tested it locally

prefix = "004"
prefix_len = len(prefix) + 1 # +1 for space
end_line_prefix = "819"
desired_value = "8041"
with open("input.txt", "r") as rfile:
    for line in rfile:
        if not line.startswith(prefix):
            continue  # if it doesnt start with 004, not interested

        val = line[prefix_len:len(line) - 1]  # at the end we also have new line
        if val != desired_value:
            continue  # if 004 doesnt have the value of our interest, skip

        while True:
            print("-> %s" % line)
            line = rfile.readline()
            if line.startswith(end_line_prefix):  # if we reached the line starting with 819, we print and then break
                print("-> %s" % line)
                break
rfile.close()

Upvotes: 1

scope
scope

Reputation: 1997

readlines reads new-line chars as well, you have to strip those before further work with your line.

Upvotes: 1

Related Questions