#! /usr/bin/env python2.7 # -*- mode: python tab-width: 4 coding: utf-8 -*- # import re text = """# excluded.txt # # $Revision: $ # $Date: 2015-04-27 19:52:22 +0100 (Mon, 27 Apr 2015) $ # # leading and trailing whitespace is ignored # comments start with a hash # other lines contain space separated NGRs either myriads, hectads or tetrads # myriads are two characters e.g. TQ # HL HM HN HO JL JM JN # myriads HQ HR HS JQ JR JS """ # simple regular expression to remove the '#' comments # from the above text ## these work # data = re.sub(r'(?m)#.*', '', text) # data = re.sub(r'#.*', '', text, re.MULTILINE|re.DEBUG) ## but this fails data = re.sub(r'#.*', '', text, re.MULTILINE) lines = data.splitlines() for line in lines: print(line)