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 problem with your current method is that <code>.strip()</code> doesn't really do what you want.  It removes leading and trailing characters (and you want to remove ones within the text), and if you want to specify characters in addition to whitespace, they need to be in a list.</p>

<p>Another problem is that there are many more potential punctuation characters (question marks, exclamations, unicode ellipses, em dashes) that wouldn't get filtered out by your list.  Instead, you can use <code>string.punctuation</code> to get a wide range of characters (note that <code>string.punctuation</code> doesn't include some non-English characters, so its viability may depend on the source of your input):</p>

<pre><code>import string
punctuation = set(string.punctuation)
text = ''.join(char for char in text if char not in punctuation)
</code></pre>

<p>An even faster method (shown in <a href="http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python">other</a> <a href="http://stackoverflow.com/questions/36464160/what-does-python-string-maketrans">answers</a> on SO) uses <code>string.translate()</code> to replace the characters:</p>

<pre><code>import string
text = text.translate(string.maketrans('', ''), string.punctuation)
</code></pre>

python python-2.7 text punctuation