I have a bunch of text files, which contain each specific parts of a larger text. Each line is referred to the composite text via a referrer (e.g., "A 42" or "A 200"). The code below works perfectly for the specified line and gives the occurrences of this particular line in the afore-mentioned text files one by one (here "A 42"):

for score in os.listdir(path): with open(os.path.join(path, score), "rb") as text: for prev_line, line in linePairs(text): if re.search("A 42", line): print line, prev_line

But, I would like to have a larger range of searches (e.g. "A 1" to "A 100") in all these files and have them printed in blocks, so all occurrences of "A 1", then all occurrences of "A 2" and so forth. I tried the following, but it doesn't work. Actually I tried to insert in the search phrase ( "A", i, line ), but this produces a syntax error.

for score in os.listdir(path): with open(os.path.join(path, score), "rb") as text: for i in range( 10, 100 ): for prev_line, line in linePairs(text): if re.search( i, line ): print line

It says: "first argument must be string or compiled pattern". I simply don't get it. I am unfortunately new to Python, but appreciate its functionality. In all fairness, I had help with the first attempt. Now, I am trying to expand everything.

for score in os.listdir(path): with open(os.path.join(path, score), "rb") as text: for i in range( 55, 60, +1 ): for prev_line, line in linePairs(text): if re.search('A {}'.format(i), line): print line, prev_line i = i + 1

Now, I get the range of lines A 55 to A 60 for each file separately. Is there a way to group all A 55 together followed by A 56 and so on?

for score in os.listdir(path): with open(os.path.join(path, score), "rb") as text: for i in range( 55, 60, +1 ): for prev_line, line in linePairs(text): if re.search('A {}'.format(i), line): print line, prev_line i = i + 1

Now, I get the range of lines A 55 to A 60 for each file separately. Is there a way to group all A 55 together followed by A 56 and so on?

Thanks for any further assistance.D.

The for loop automatically increments <i>, so you do not nee the "i = i + 1" line. This leads to confusion (not just for humans, but the computer as well).

Why not use a dictionary? If I understand your end-goal, you just want to identify the line number of each file that corresponds to a given "A #" value (in your code I do not understand where <prev_line> comes into play, nor do I know what <linePairs> is, but I will give it a go anyway).

for score in os.listdir(path): text = open(os.path.join(path,score),'rb').read().split('\n') # Assuming EOL = '\n' and "A #" is only contents of each line; can modify as needed for r in range(55,60): # Bad form to use <i>, in general if 'A ' + str(r) in text: tmp[r].append( (fileNumber,text.index('A '+str(r))) ) #I don't know how you define the file number, so I leave this generic