
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 0.95 KB | hits: 10 | expires: Never
Python: delete the words between two delimeters
<@ """@$ FSDF >something something <more noise>
>>> import re
>>> s = '<@ """@$ FSDF >something something <more noise>'
>>> re.sub('<[^>]+>', '', s)
'something something '
import re
s = #your string here
t = re.sub('<.*?>', '', s)
import re
my_str = '<@ """@$ FSDF >something something <more noise>'
re.sub('<.*?>', '', my_str)
'something something '
with open('blah.txt','w') as f:
f.write("""<sdgsa>one<as<>asfd<asdf>
<asdf>two<asjkdgai><iasj>three<fasdlojk>""")
def filter_line(line):
count=0
ignore=False
result=[]
for c in line:
if c==">" and count==1:
count=0
ignore=False
if not ignore:
result.append(c)
if c=="<" and count==0:
ignore=True
count=1
return "".join(result)
with open('blah.txt') as f:
print "".join(map(filter_line,f.readlines()))
>>>
<>one<>asfd<>
<>two<><>three<>