-2

I'm trying to remove large blocks of text from a file using python. Each block of text begins with

/translation="SOMETEXT"

Ending with the second quote.

Can anyone give me some advice on how to accomplish this?

Thank you

2
  • Use a regular expression. I'm not much into those but it's just a hint into the right direction. Commented Feb 17, 2012 at 15:35
  • Try the re module, use some regex tester it will make tests easier (ksamuel.pythonanywhere.com). Commented Feb 17, 2012 at 16:24

2 Answers 2

1

You can use re.sub like this:

import re
re.sub("/translation=\".*?\" ", "", s)
Sign up to request clarification or add additional context in comments.

Comments

0

If performance doesn't matter, you could do something like this. Regular expressions would probably be faster, but this is simpler.

def remtxt(s,startstr,endstr):
        while startstr in s:
                startpos=s.index(startstr)
                try:
                        endpos=s.index(endstr,startpos+len(startstr))+len(endstr)
                except:
                        return
                s=s[:startpos]+s[endpos:]
        return s

new_string=remtxt(my_string,'/translation="','"')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.