简体   繁体   中英

Removing blocks of text with python

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

You can use re.sub like this:

import re
re.sub("/translation=\".*?\" ", "", s)

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="','"')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM