Jump to content

Python 3.5 removing punctuation

So I've seen some things on stack overflow and such on how to remove punctuation from a string, but I think I'm missing something. I just need to remove things like commas, quotation marks, slashes, etc. I can't really seem to figure out a good way to do it other than the obvious replace them all individually with "". So is there a good way to do it that someone can explain to me?

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Lunar Evolution said:

So I've seen some things on stack overflow and such on how to remove punctuation from a string, but I think I'm missing something. I just need to remove things like commas, quotation marks, slashes, etc. I can't really seem to figure out a good way to do it other than the obvious replace them all individually with "". So is there a good way to do it that someone can explain to me?

I think there's a function you can use to check if a character is alphanumeric. If it isn't, replace with "".

Link to comment
Share on other sites

Link to post
Share on other sites

replacing each with "" is the correct way to do it

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Simply iterate through the array and check against unwanted characters. Replace with "" and your done.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Enderman said:

replacing each with "" is the correct way to do it

I found ways that seemed to use something to do with the .translate() function or things similar. Is that incorrect?

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Lunar Evolution said:

I found ways that seemed to use something to do with the .translate() function or things similar. Is that incorrect?

never heard of .translate()

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Lunar Evolution said:

I found ways that seemed to use something to do with the .translate() function or things similar. Is that incorrect?

translate would work fine, which one is faster will depend on your data.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Spoiler

str = "it's bit me. I'm good"

print str.replace("'", "")

no code tags as I'm in my phone.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×