2

I need to remove the punc from a text file.

The text file is like this

ffff, hhhh, & tommorw home,
Have you from gone?

I am trying

punc=(",./;'?&-")

f = open('file.txt', 'r')

for line in f:
    strp=line.replace(punc,"")
    print(strp)

I need the output to be:

ffff hhhh tommorw home

Have you from gone

This is returning each line, but the punc is still there> could use some help. Thanks

nneonneo
  • 171,345
  • 36
  • 312
  • 383
Trying_hard
  • 8,931
  • 29
  • 62
  • 85
  • 2
    possible duplicate of [Best way to strip punctuation from a string in Python](http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python) – Burhan Khalid Sep 17 '12 at 05:38
  • Do you want **all** punctuation stripped, regardless of where it appears, or only at word boundaries? – Lukas Graf Sep 17 '12 at 05:45
  • i need all gone anywhere in the text file – Trying_hard Sep 17 '12 at 05:51
  • related: [Remove punctation from Unicode formatted strings](http://stackoverflow.com/q/11066400/4279) – jfs Sep 17 '12 at 05:55
  • Does this answer your question? [Removing Punctuation From Python List Items](https://stackoverflow.com/questions/4371231/removing-punctuation-from-python-list-items) – Abu Shoeb Nov 15 '20 at 23:40

4 Answers4

9

Use str.translate to delete characters from a string.

In Python 2.x:

# first arg is translation table, second arg is characters to delete
strp = line.translate(None, punc)

In Python 3:

# translation table maps code points to replacements, or None to delete
transtable = {ord(c): None for c in punc}
strp = line.translate(transtable)

Alternately, you can use str.maketrans to build transtable:

# first and second arg are matching translated values, third arg (optional) is the characters to delete
transtable = str.maketrans('', '', punc)
strp = line.translate(transtable)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
3
>>> import string
>>> with open('/tmp/spam.txt') as f:
...   for line in f:
...     words = [x.strip(string.punctuation) for x in line.split()]
...     print ' '.join(w for w in words if w)
... 
ffff hhhh tommorw home
Have you from gone
wim
  • 338,267
  • 99
  • 616
  • 750
  • That is an oddly complicated way of solving this problem. Why are you only removing punctuation at the start and end of segments (x.strip())? – Lukas Graf Sep 17 '12 at 05:40
  • That's how I understood the problem, I didn't want for example hyphenated words or words with apostrophes to be mangled. – wim Sep 17 '12 at 05:42
  • Oh. I would have expected the OP wants the hyphens stripped from them as well, but it's not clear for the question now that I look at it again. – Lukas Graf Sep 17 '12 at 05:44
0
import string

str_link = open('replace.txt','r').read()

#str_link = "ffff, hhhh, & tommorow home, Have you from gone?"

punc = list(",./;'?&-")

for line in str_link:
    if line in punc:
        str_link = str_link.replace(line,"") 

print str_link
Ravi Pal
  • 395
  • 1
  • 7
  • 22
0

I think the idea of using str.translate is great, but here's one other way to do it:

punc=set(",./;'?&-")

for line in f:
    strp=''.join(c for c in line if not c in punc)
    print(strp)
Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48