#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Multiple textfiles, grouping results


    I have several text files in one folder that need to be searched systematically after line referrers (e.g., A 33). These references link to a composite text. Following three examples for these text files:

    Code:
     #text file no 1
    1. line 1
    >>A 33
    2. line 2
    >>A 34
    3. line 3
    >>A 35
    Code:
     #text file no 2
    1. line 1
    >>A 34
    2. line 2
    >>A 35
    3. line 3
    >>A 36
    Code:
     #text file no 3
    1. line 1
    >>A 36
    2. line 2
    >>A 37
    3. line 3
    >>A 38
    With some help I got to the following code (part) to search and return the lines in range:

    Code:
        for score in os.listdir(path):
            with open(os.path.join(path, score), "rb") as text:
                for prev_line, line in linePairs(text):
                	for i in range( 180, 185 ):
                   		if re.search('A {}'.format(i), line):
                	    		print('{}{}'.format(line, prev_line))
    The problem is, that I get the results, but they are not grouped according to the A-numbers. The code yields all search results given in the range and treats every file separately.

    I would like to have all results grouped with the header A 181, then A 182 and so on, yielding an output like the following (for the afore-mentioned text-files)

    Code:
     #Output
    >>A 33
    1. Line 1 # file no 1
    
    >>A 34
    2. Line 2 # file no 1
    1. Line 1 # file no 2
    
    >>A 35
    3. Line 3 # file no 1
    2. Line 2 # file no 2
    
    >>A 36
    3. Line 3 # file no 2
    1. Line 1 # file no 3
    
    >>A 37
    2. Line 2 # file no 3
    
    >>A 38
    3. Line 3 # file no 3
    I tried re-ordering the lines in the code and also changing the indentation. Thanks for any advice.
    D.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I'm really not very clear on what your doing in the part that you say works. For instance, why are you opening a text file as binary? Putting that aside, since you seem to be happy with it, I think you just need to put your results in a dictionary that groups them however you like, i.e., d["A_33"]=("Line....", "Line..."), and then print the dictionary when you're done.

IMN logo majestic logo threadwatch logo seochat tools logo