Removing code from a vrml (wrl) file, selectively
<p>I have a vrml (wrl) file that I need to remove chucks of text from. The chunks of text to be removed can be identified by match1 and match2.</p>

<p>The chunks of text to be removed should be from match1, up to but excluding match2. Note match1 should be excluded from removal also.</p>

<p>So given the following, where match1 is "color [" and match2 is "] #color":</p>

<pre><code>...
a
b
color [
0 0 0
0 0 0
0 0 0
] #color
c
d
...
</code></pre>

<p>With the following code provided by Levon:</p>

<pre><code>with open('try1a.wrl') as inf:
    ignoreLines = False
    for line in inf:
        if 'color [' in line:
            print line,
            ignoreLines = True
        if '] #color' in line:
            ignoreLines = False            
        if not ignoreLines:
            print line,
</code></pre>

<p>I get the following:</p>

<pre><code>a
b
color [
] #color
c
d
</code></pre>

<p>Which is exaclty what I want.</p>

<p>However, I have discovered there exists one other set of these chunks of text that begins
and ends with match1 and match2, respectively, that should not be removed.  The difference between the chunks I want to remove, and the chunks I don't want to remove, is the line which follows match1.</p>

<p>So if the code see the following, it should pass over this chuck without removing anything, and move on to the next:</p>

<pre><code>...
a
b
color [
0 0 1     # since this is not "0 0 0" the code should leave this unchanged
0 0 0
0 0 0
] #color
c
d
...
</code></pre>

<p>So to summaize, if the code sees "0 0 0" after match1, then remove all lines up to match2 (which the code currently does fine), but if the code does not see a "0 0 0" leave this alone and move on to the next. This latter functionality is what needs to be added to the code provided by Levon. By the way thank you Levon for this code!</p>

<p>Edit:</p>

<p>Here's the finished product and it works fantastic, again thanks to you all who helped me:</p>

<pre><code>import string
import sys
import re
import subprocess

file_name_in = sys.argv[1]
file_name_out = sys.argv[2]

f = file(file_name_out, 'w')
sys.stdout = f

with open(file_name_in) as inf:
    ignoreLines = False
    for line in inf:
        if 'color [' in line:
            print line,
            line = next(inf, '')
            ignoreLines = all((s == '0' or s == '0,') for s in line.split())
        if '] #color' in line:
            ignoreLines = False            
        if not ignoreLines:
            print line,

sys.stdout = sys.__stdout__
f.close()

# remlns8.py inputFile.wrl outputFile.wrl
</code></pre>
