0
Beginning

Line 2
Line 3
Line 4
Line 5
Line 6
End

Trying to pull off line 2 through line 6. Can't do it to save my soul. a is the saved string I'm searching through.

b = re.findall(r'Beginning(.*?)End', a)

Doesn't give me a thing, just a blank b. I know it's because of the newlines but how do I go about detecting and moving on forward with the newlines. I've tried, not knowing exactly for sure how I'm suppose to use MULTILINE or DOTALL. Nothing changed.

How do I go about getting it to put lines 2 through 6 in b?

To add in this will occur multiple times through the same file that I need to perform this search and pull technique. I have no other easy way of doing this since the information in Lines 2-6 need to be looked through further to pull off data that will be put into a csv file. Since some of the data contains hours and some of the data doesn't contain hours, aka Unavailable, I need to be able to pull off and differentiate between the two occurrences.

confused
  • 1,283
  • 6
  • 21
  • 37

2 Answers2

0
string = """Beginning

Line 2
Line 3
Line 4
Line 5
Line 6
End
"""

lines = string.splitlines()
answer = []
flag = False
for line in lines:
    line = line.strip()
    if not line: continue
    if line == "Beginning":
        flag = True
        continue
    if line == "End": flag = False
    if not flag: continue
    answer.append(line)

Output:

In [209]: answer
Out[209]: ['Line 2', 'Line 3', 'Line 4', 'Line 5', 'Line 6']
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • Is there a way of doing without splitting the lines? I know I will have several occurrences of this while searching through the file. I need to be able to differentiate between occurrence so I can place everything with the proper group. Above will let me pull off the each occurrence but trying to split between the occurrence is going to be worse than a nightmare thanks to each occurrence not always being the same number of lines. – confused Nov 22 '16 at 00:43
0

You could make a function that takes a multi-line string, then a starting line, and an ending line.

def Function(string, starting_line, ending_line):
    if "\n" in string: #Checks for whether or not string is mult-line
        pass
    else:
        return "The string given isn't a multiline string!" #If string isn't multiline, then Function returns a string explaining that string isn't a multi-line string
    if ending_line < starting_line: #Checks if ending_line < starting_line
        return "ending_line is greater than starting_line!" #If ending_line < starting_line, then Function returns a string explaining that ending_line > starting_line
    array = [] #Defines an array
    for i in range(len(string)): #Loops through len(string)
        if list(string)[i] = "\n": #Checks whether list(string)[i] = a new line
            array.append(i) #Appends i to array
    return string[array[starting_line - 1]::array[ending_line - 1]]
print(Function(a, 3, 7))

This code should return:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
MilkyWay90
  • 2,023
  • 1
  • 9
  • 21