0

I am trying to delete all punctuation marks from a sentence in python, but when i use this code:

 myString.translate(None, string.punctuation)

I get the error:

translate() takes exactly one argument (2 given)

and I couldn't solve the reason for the error.

Pela647
  • 31
  • 9
  • That's the syntax for Python 2.x - are you using Python 3.x per chance? – Jon Clements Apr 26 '16 at 15:42
  • @Jon Clements: yes I am using Python 3.x – Pela647 Apr 26 '16 at 15:45
  • More specifically, that's the syntax for byte strings. Unicode on python2.x wouldn't work here either, and byte-strings on python3.x do work (provided that you encode `string.punctuation` with the `'ascii'` codec ...) – mgilson Apr 26 '16 at 15:47
  • take a look at: http://stackoverflow.com/a/34294398/797495 – Pedro Lobito Apr 26 '16 at 15:49
  • Shucks... Had a cross-version answer ready but then the question was closed – jDo Apr 26 '16 at 15:52
  • 1
    @PedroLobito ! Thanks works great now! – Pela647 Apr 26 '16 at 16:00
  • I'm glad it worked for you @Pela647 ! – Pedro Lobito Apr 26 '16 at 16:01
  • @PedroLobito But now i have additional problem- I have done as the above stackoverflow link suggests, but if there was no space between the punctuation mark and the word, the output becomes, one joined word; for example; input: " i am,a" , output: "i ama" instead I want the output to be "i am a". – Pela647 Apr 26 '16 at 16:22

1 Answers1

0

Remove all punctuation from a string:

import string
transtable = {ord(c): None for c in string.punctuation}
strp = line.translate(transtable)
strp

Example:

Input : "Hey, Lets check this out!?"

Output : 'Hey Lets check this out'

Ani Menon
  • 27,209
  • 16
  • 105
  • 126