Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.95 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Python: delete the words between two delimeters
  2. <@ """@$ FSDF >something something <more noise>
  3.        
  4. >>> import re
  5. >>> s = '<@ """@$ FSDF >something something <more noise>'
  6. >>> re.sub('<[^>]+>', '', s)
  7. 'something something '
  8.        
  9. import re
  10. s = #your string here
  11. t = re.sub('<.*?>', '', s)
  12.        
  13. import re
  14. my_str = '<@ """@$ FSDF >something something <more noise>'
  15. re.sub('<.*?>', '', my_str)
  16. 'something something '
  17.        
  18. with open('blah.txt','w') as f:
  19.     f.write("""<sdgsa>one<as<>asfd<asdf>
  20. <asdf>two<asjkdgai><iasj>three<fasdlojk>""")
  21.  
  22. def filter_line(line):
  23.     count=0
  24.     ignore=False
  25.     result=[]
  26.     for c in line:
  27.         if c==">" and count==1:
  28.             count=0
  29.             ignore=False
  30.         if not ignore:
  31.             result.append(c)
  32.         if c=="<" and count==0:
  33.             ignore=True
  34.             count=1
  35.     return "".join(result)
  36.  
  37. with open('blah.txt') as f:
  38.     print "".join(map(filter_line,f.readlines()))
  39.  
  40. >>>
  41. <>one<>asfd<>
  42. <>two<><>three<>