0

次の形式のコンテンツを含む可能性のある一連のテキスト ファイルを検索する必要があります。

//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

2行の間の部分を削除して保存します。

テキストを複数行に分割して、1行ずつチェックするので作業が重くなるのですが、簡単にできる方法はありませんか?

4

2 に答える 2

4

これをチェックしてください

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
于 2013-09-16T05:39:13.393 に答える
2

正規表現を使用して、一致行を空の行に置き換えることができます。@yuwang はsedバージョンを提供しました。これがpythonバージョンです( 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
于 2013-09-16T06:29:43.567 に答える