i need to search a bunch of text files which may contain content in this forms:
//for nv version
tf = new TextField();
tf.width = 600;
tf.height = 300;
tf.textColor = 0x00ffff;
tf.mouseEnabled = false;
this.addChild(tf);
Sys.__consoleWindowFuncBasic = log;
//nv end
and delete the part between the 2 lines and save them.
i split the text into lines, and check the text line by line,thus make the work very heavy,is there any easy way for doing this?
Check this out
beginMarker = "//for nv version"
endMarker = "//nv end"
include = True
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
for line in infile:
if include:
if line.strip() != beginMarker:
outfile.write(line)
else:
include = False
else:
if line.strip() == endMarker:
include = True
Maybe you can try to use the regular expression to replace the match lines to an empty line. @yuwang provided a sed
version, here is a python
version( python re docs ):
>>> import re
>>> s = """some words
... other words
... //for nv version
... tf = new TextField();
... tf.width = 600;
... tf.height = 300;
... tf.textColor = 0x00ffff;
... tf.mouseEnabled = false;
... this.addChild(tf);
... Sys.__consoleWindowFuncBasic = log;
... //nv end
... yet some other words
... """
>>> p = r"//for nv version(.*?)//nv end\n" # reluctant instead of greedy
>>> print re.sub(p, "", s, flags=re.S) # use re.S to make `.` matchs `\n`
some words
other words
yet some other words
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.