Python - Find A Portion Of A Text File To Export As Data To Manipulate
I have a text file and from it I want to extract everything below a key word and nothing above another word. I particularly care about numeric values inside this portion of text. L
Solution 1:
I guess this should work, but you can improve the code!
import re
data = []
in_file = False
with open('sample.txt', 'r') as infile:
     for line in infile:
          if "ROI: CTV" in line: in_file = True
          if "ROI: PTV" in line: in_file = False
          if in_file:
              line = line.split(' ')
              for item in line:
                  if re.search(r'(?<![a-zA-Z:])[-+]?\d*\.?\d+', item):
                      match = re.search(r'(?<![a-zA-Z:])[-+]?\d*\.?\d+', item)
                      data.append(match.group(0))
print data
Post a Comment for "Python - Find A Portion Of A Text File To Export As Data To Manipulate"