Python Removing full sentences of words from a novel-long string
<p>I have pasted a novel into a text file.
I would like to remove all the lines containing of the following sentences as they keep occurring at the top of each page (just removing their occurrences in those lines will do as well):</p>

<blockquote>
  <p>"Thermal Molecular Movement in , Order and Probability" </p>
  
  <p>"Molecular and Ionic Interactions as the Basis for the Formation" </p>
  
  <p>"Interfacial Phenomena and Membranes"</p>
</blockquote>

<p>My first attempt is as follows:</p>

<pre><code>mystring = file.read()
mystring=mystring.strip("Molecular Structure of Biological Systems")
mystring=mystring.strip("Thermal Molecular Movement in , Order and Probability")
mystring=mystring.strip("Molecular and Ionic Interactions as the Basis for the Formation")
mystring=mystring.strip("Interfacial Phenomena and Membranes")

new_file=open("no_refs.txt", "w")

new_file.write(mystring)

file.close()
</code></pre>

<p>However this had no effect on the output text file... the contents were completely unchanged... I find this strange as the following toy example works fine:</p>

<pre><code>&gt;&gt;&gt; "Hello this is a sentence. Please read it".strip("Please read it")
'Hello this is a sentence.'
</code></pre>

<p>As the above didn't work I tried the following instead:</p>

<pre><code>file=open("novel.txt", "r")
mystring = file.readlines()
for lines in mystring:
    if "Thermal Molecular Movement in , Order and Probability" in lines:
        mystring.replace(lines, "")
    elif "Molecular and Ionic Interactions as the Basis for the Formation" in lines:
        mystring.replace(lines, "")
    elif "Interfacial Phenomena and Membranes" in lines:
        mystring.replace(lines, "")
    else:
        continue

new_file=open("no_refs.txt", "w")

new_file.write(mystring)
new_file.close()
file.close()
</code></pre>

<p>But for this attempt I get this error:</p>

<p>TypeError: expected a string or other character buffer object</p>

<ul>
<li>First <code>str.strip()</code> only removes the pattern if found at the <em>start</em> or the <em>end</em> of the string which explains that it seems to work in your test, but in fact is not what you want.</li>
<li>Second, you're trying to perform a replace on a list not on the current line (and you don't assign back the replacement result)</li>
</ul>

<p>Here's a fixed version which successfully removes the patterns of the lines:</p>

<pre><code>with open("novel.txt", "r") as file:
    mystring = file.readlines()
    for i,line in enumerate(mystring):
        for pattern in ["Thermal Molecular Movement in , Order and Probability","Molecular and Ionic Interactions as the Basis for the Formation","Interfacial Phenomena and Membranes"]:
            if pattern in line:
                mystring[i] = line.replace(pattern,"")                    

    # print the processed lines
    print("".join(mystring))
</code></pre>

<p>Note the <code>enumerate</code> construct, which allows to iterate on the values &amp; index. Iterating only on the values would allow to find the patterns but not to modify them in the original list.</p>

<p>Also note the <code>with open</code> construct, that closes the file as soon as you leave the block.</p>

<p>Here's a version which completely removes the lines containing the patterns (hang on, there's some one-liner functional programming stuff in there):</p>

<pre><code>with open("novel.txt", "r") as file:
    mystring = file.readlines()
    pattern_list = ["Thermal Molecular Movement in , Order and Probability","Molecular and Ionic Interactions as the Basis for the Formation","Interfacial Phenomena and Membranes"]
     mystring = "".join(filter(lambda line:all(pattern not in line for pattern in pattern_list),mystring))
    # print the processed lines
    print(mystring)
</code></pre>

<p>explained: filter list of lines according of the condition: none of the unwanted patterns must be in the line.</p>

python string