How can I travel through the words of a file in PYTHON?
<p>I have a file .txt and I want to travel through the words of it. I have a problem, I need to remove the punctuation marks before travelling through the words. I have tried this, but it isn't removing the punctuation marks.</p>

<pre><code>file=open(file_name,"r")
for word in file.read().strip(",;.:- '").split():
     print word
file.close()
</code></pre>

<p>The following code preserves apostrophes and blanks, and could easily be modified to preserve double quotations marks, if desired. It works by using a translation table based on a subclass of the string object. I think  the code is fairly easy to understand. It might be made more efficient if necessary.</p>

<pre><code>class SpecialTable(str):
    def __getitem__(self, chr):
        if chr==32 or chr==39 or 48&lt;=chr&lt;=57 \
            or 65&lt;=chr&lt;=90 or 97&lt;=chr&lt;=122:
            return chr
        else:
            return None

specialTable = SpecialTable()


with open('temp2.txt') as inputText:
    for line in inputText:
        print (line)
        convertedLine=line.translate(specialTable)
        print (convertedLine)
        print (convertedLine.split(' '))
</code></pre>

<p>Here's typical output.</p>

<pre><code>This! is _a_ single (i.e. 1) English sentence that won't cause any trouble, right?

This is a single ie 1 English sentence that won't cause any trouble right
['This', 'is', 'a', 'single', 'ie', '1', 'English', 'sentence', 'that', "won't", 'cause', 'any', 'trouble', 'right']
'nother one.

'nother one
["'nother", 'one']
</code></pre>

python python-2.7 text punctuation