Regex + Python - Remove All Lines Beginning With A *
I want to remove all lines from a given file that begin with a *. So for example, the following: * This needs to be gone But this line should stay *remove * this too End Should g
Solution 1:
Solution 2:
You don't need regex for this.
text = file.split('\n') # split everything into lines.for line in text:
# do something here
Let us know if you need any more help.
Solution 3:
You should really give more information here. At the minimum, what version of python you are using and a code snippet. But, that said, why do you need a regular expression? I don't see why you can't just use startswith.
The following works for me with Python 2.7.3
s = '* this line gotta go!!!'print s.startswith('*')
>>>True
Solution 4:
>>> f = open('path/to/file.txt', 'r')
>>> [n for n in f.readlines() ifnot n.startswith('*')]
['But this line should stay\n', 'End\n']
Post a Comment for "Regex + Python - Remove All Lines Beginning With A *"