What does this do in python?
df['Name'].str.replace('\W', ' ')
df['Name'].str.replace('_', ' ')
df['Name'].str.replace(' ', ' ')
Does \W remove "=" from a string? I have a script and I'm trying to figure out where the "=", ",", and more get removed from my strings.
Can anyone explain what would remove this punctuation?
CodePudding user response:
The part of replacing with punctuation can also be performed using regex. In this, we replace all punctuation with an empty string using a certain regex
# Python3 code to demonstrate working of
# Removing punctuations in string
# Using regex
import re
# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
# printing original string
print("The original string is : " test_str)
# Removing punctuations in string
# Using regex
res = re.sub(r'[^\w\s]', '', test_str)
# printing result
print("The string after punctuation filter : " res)
OR
# Python3 code to demonstrate working of
# Removing punctuations in string
# Using loop punctuation string
# initializing string
test_str = "Gfg, is best : for ! Geeks ;"
# printing original string
print("The original string is : " test_str)
# initializing punctuations string
punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# Removing punctuations in string
# Using loop punctuation string
for ele in test_str:
if ele in punc:
test_str = test_str.replace(ele, "")
# printing result
print("The string after punctuation filter : " test_str)
CodePudding user response:
\W (upper case W) matches any non-word character. This means not only "=" is replaced but any non-word character, e.g. "%","&","§","/",...