Traker Traker - 1 year ago 73
Python Question

How can I travel through the words of a file in PYTHON?

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.

for word in",;.:- '").split():
print word

Answer Source

The problem with your current method is that .strip() 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.

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 string.punctuation to get a wide range of characters (note that string.punctuation doesn't include some non-English characters, so its viability may depend on the source of your input):

import string
punctuation = set(string.punctuation)
text = ''.join(char for char in text if char not in punctuation)

An even faster method (shown in other answers on SO) uses string.translate() to replace the characters:

import string
text = text.translate(string.maketrans('', ''), string.punctuation)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download