Python Regex: How To Remove Punctuation

preview_player
Показать описание
↓ Code Available Below! ↓

This video shows how to match and strip punctuation from a text string using the regular expressions package in Python. Text data often contains punctuation that you might want to strip out of text as a preprocessing step before splitting the text into its constituent words for data analysis. The Python regular expressions package makes it easy to match and replace punctuation with empty strings, effectively stripping out all punctuation.

If you find this video useful, like, share and subscribe to support the channel!

Code used in this Python Code Clip:

import re

lines = '''
Nappa @ Vegeta: What does the scouter say about his power level?
Vegeta @ Nappga: It's over (9000!)
Nappa @ Vegeta: [What 9000?] That can't be right... Can it?'''

repl = "",
string = lines)

repl = "",
string = lines)

* Note you can access some common punctuation characters using:

import string

** Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .

Рекомендации по теме
Комментарии
Автор

Code used in this video:

import re

lines = '''
Nappa @ Vegeta: What does the scouter say about his power level?
Vegeta @ Nappga: It's over (9000!)
Nappa @ Vegeta: [What 9000?] That can't be right... Can it?
'''

# Match and strip punctuation with re.sub()
re.sub(pattern = "[^\w\s]",
repl = "",
string = lines)

# Match and strip punctuation and whitespace with re.sub()
re.sub(pattern = "\W",
repl = "",
string = lines)

* Note you can access some common punctuation characters using:

import string
string.punctuation

DataDaft
Автор

Yet, another Python guy who gets it! thank you!

johnyf.q.
Автор

How do I remove punctuation from a column in a dataframe? Here is what I tried:
import re
re.sub(pattern = "[^\W\S]",
repl = " ",
string = elon_mentions['content'] )

CaribouDataScience
Автор

Amazing video!!!!
Can you please explain how to remove only punctuation instead of whitespace

polamaithreya
Автор

Thank you so much for this video...can you please make a video on cleansing of twitter data ?? like tweets that remove username, numbers etc . ready data for sentimental analysis and stuffs?? Stay safe ..thanks again!

dikshyantthapa
Автор

so I am a bit confused, I used it and this thing did the exact opposite.

elmacron