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>You can try using the <code>re</code> module:</p>

<pre><code>import re
with open(file_name) as f:
    for word in re.split('\W+', f.read()):
        print word
</code></pre>

<p>See the <a href="https://docs.python.org/2/library/re.html" rel="nofollow">re documentation</a> for more details.</p>

<p>Edit: In case of non ASCII characters, the previous code ignore them. In that case the following code can help:</p>

<pre><code>import re
with open(file_name) as f:
    for word in re.compile('\W+', re.unicode).split(f.read().decode('utf8')):
        print word
</code></pre>

python python-2.7 text punctuation