Python cannot read a file which contains a specific string
<p>I've written a function to remove certain words and characters for a string. The string in question is read into the program using a file. The program works fine except when a file, anywhere, contains the following anywhere in the body of the file.</p>

<blockquote>
  <p>Security Update for Secure Boot (3177404) This security update
  resolves a vulnerability in Microsoft Windows. The vulnerability could
  allow Secure Boot security features to be bypassed if an attacker
  installs an affected policy on a target device. An attacker must have
  either administrative privileges or physical access to install a
  policy and bypass Secure Boot.</p>
</blockquote>

<p>I've never experienced such weird behavior. Anybody have any suggestions?</p>

<p>This is the function I've written.</p>

<pre><code>def scrub(file_name):
    try:
        file = open(file_name,"r")
        unscrubbed_string = file.read()
        file.close()

        cms = open("common_misspellings.csv","r")
        for line in cms:
            replacement = line.strip('\n').split(',')
            while replacement[0] in unscrubbed_string:
                unscrubbed_string = unscrubbed_string.replace(replacement[0],replacement[1])

        cms.close()

        special_chars = ['.',',',';',"'","\""]

        for char in special_chars:
            while char in unscrubbed_string:
                unscrubbed_string = unscrubbed_string.replace(char,"")

        unscrubbed_list = unscrubbed_string.split()

        noise = open("noise.txt","r")
        noise_list = []

        for word in noise:
            noise_list.append(word.strip('\n'))

        noise.close()

        for noise in noise_list:
            while noise in unscrubbed_list:
                    unscrubbed_list.remove(noise)
        return unscrubbed_list

    except:
        print("""[*] File not found.""")
</code></pre>

<p>Your code may be hanging because your <code>.replace()</code> call is in a <code>while</code> loop. If, for any particular line of your <code>.csv</code> file, the <code>replacement[0]</code> string is a <em>substring</em> of its corresponding <code>replacement[1]</code>, and if either of them appears in your critical text, then the <code>while</code> loop will never finish.  In fact, you don't need the <code>while</code> loop at allâa single <code>.replace()</code> call will replace all occurrences.</p>

<p>But that's only one example of the problems you'll encounter with your current approach of using a blanket <code>unscrubbed_string.replace(...)</code>   You'll either need to use <em>regular expression</em> substitution (from the <code>re</code>) module, or break your string down into words yourself and work word-by-word instead.  Why?  Well, here's a simple example: <code>'Teh'</code> needs to be corrected to <code>'The'</code>âbut what if the document contains a reference to <code>'Tehran'</code>? Your "Secure Boot" text will contain an example analogous to this.</p>

<p>If you go the regular-expression route, the symbol <code>\b</code> solves this by matching word boundaries of any kind (start or end of string, spaces, punctuation).  Here's a simplified example:</p>

<pre><code>import re

replacements = {
    'Teh':'The',
}
unscrubbed = 'Teh capital of Iran is Tehran. Teh capital of France is Paris.'

better = unscrubbed
naive = unscrubbed
for target, replacement in replacements.items():
    naive = naive.replace(target, replacement)

    pattern = r'\b' + target + r'\b'
    better = re.sub(pattern, replacement, better)

print(unscrubbed)
print(naive)
print(better)
</code></pre>

<p>Output, with mistakes emphasized:</p>

<blockquote>
  <p><strong><em>Teh</em></strong> capital of Iran is Tehran. <strong><em>Teh</em></strong> capital of France is Paris. (<code>unscrubbed</code>)</p>
  
  <p>The capital of Iran is <strong><em>Theran</em></strong>. The capital of France is Paris. (<code>naive</code>)</p>
  
  <p>The capital of Iran is Tehran. The capital of France is Paris. (<code>better</code>)</p>
</blockquote>

python file python-3.x