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><code>strip()</code>only removes characters found at the beginning or end of a string.
So <code>split()</code> first to cut into words, then <code>strip()</code> to remove punctuation.</p>

<pre><code>import string

with open(file_name, "rt") as finput:
    for line in finput:
        for word in line.split():
            print word.strip(string.punctuation)
</code></pre>

<p>Or use a natural language aware library like <code>nltk</code>: <a href="http://www.nltk.org/" rel="nofollow">http://www.nltk.org/</a></p>

python python-2.7 text punctuation