One way is to use re.sub, that's my preferred way.
import re
my_str = "hey th~!ere"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string
Output:
hey there
Another way is to use re.escape:
import string
import re
my_str = "hey th~!ere"
chars = re.escape(string.punctuation)
print re.sub('['+chars+']', '',my_str)
Output:
hey there
Just a small tip about parameters style in python by PEP-8 parameters should be remove_special_chars and not removeSpecialChars
Also if you want to keep the spaces just change [^a-zA-Z0-9 \n\.] to [^a-zA-Z0-9\n\.]
One way is to use re.sub, that's my preferred way.
import re
my_str = "hey th~!ere"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string
Output:
hey there
Another way is to use re.escape:
import string
import re
my_str = "hey th~!ere"
chars = re.escape(string.punctuation)
print re.sub('['+chars+']', '',my_str)
Output:
hey there
Just a small tip about parameters style in python by PEP-8 parameters should be remove_special_chars and not removeSpecialChars
Also if you want to keep the spaces just change [^a-zA-Z0-9 \n\.] to [^a-zA-Z0-9\n\.]
str.replace is the wrong function for what you want to do (apart from it being used incorrectly). You want to replace any character of a set with a space, not the whole set with a single space (the latter is what replace does). You can use translate like this:
removeSpecialChars = z.translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})
This creates a mapping which maps every character in your list of special characters to a space, then calls translate() on the string, replacing every single character in the set of special characters with a space.
python - I want to replace a special character with a space - Stack Overflow
Def Function to Remove Special Characters from User Input and Replace with space " "
Special Characters in str.replace()
python replace space with special characters between strings - Stack Overflow
Assuming you mean to change everything non-alphanumeric, you can do this on the command line:
cat foo.txt | sed "s/[^A-Za-z0-99]/ /g" > bar.txt
Or in Python with the re module:
import re
original_string = open('foo.txt').read()
new_string = re.sub('[^a-zA-Z0-9\n\.]', ' ', original_string)
open('bar.txt', 'w').write(new_string)
import string
specials = '-"/.' #etc
trans = string.maketrans(specials, ' '*len(specials))
#for line in file
cleanline = line.translate(trans)
e.g.
>>> line = "Indo-American pvt/ltd"
>>> line.translate(trans)
'Indo American pvt ltd'
Your call to dict.fromkeys() does not include the character / in its argument.
If you want to map all the special characters to None, just passing your list of special chars to dict.fromkeys() should be enough. If you want to replace them with a space, you could then iterate over the dict and set the value to for each key.
For example:
special_chars = "?:/"
special_char_dict = dict.fromkeys(special_chars)
for k in special_char_dict:
special_char_dict[k] = " "
You can do this by extending your translation table:
dex = ["My Name is/Richard????::,"]
table = str.maketrans({'?':None,':':None,',':None,'/':' '})
for index, name in enumerate(dex, start = 0):
print('{}.{}'.format(index, name.strip().translate(table)))
OUTPUT
0.My Name is Richard
You want to replace most special characters with None BUT forward slash with a space. You could use a different method to replace forward slashes as the other answers here do, or you could extend your translation table as above, mapping all the other special characters to None and forward slash to space. With this you could have a whole bunch of different replacements happen for different characters.
This is what my module file looks like with my functions, def ignore_function is the one I've created for this particular problem
import random
# These are my responses I created in a separate file so that my code would
# be more clean
# They are sepeared by positive, neutral, and negative responses
# The last function called magicball_all() just conjoins them and picks a random response
def magicball_pos():
pos_resp = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes"
]
return (pos_resp)
def magicalball_neut():
neut_resp = [
"Reply hazy, try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again"
]
return (neut_resp)
def magicalball_neg():
neg_resp = [
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
]
return (neg_resp)
def magicball_all():
all_resp = [
magicball_pos,
magicalball_neut,
magicalball_neg
]
random_func = random.choice(all_resp)
return random_func()
def ignore_punct(text):
remove_punc = ["!", ".", ",", "?"]
new_text = text
for char in remove_punc:
new_text = new_text.replace(char, " ")
return new_text
This is my actual program file, with the line:
if ask == "will I become rich":
I need it to ignore any capitals as it will not trigger special even
I also need it to ignore for example if you said "WILL I BECOME RICH????" it will not trigger
or "will I become rich?!" it will not trigger.
So I need it to ignore case sensitivity as well as punctuation. I'm hoping I can make a function
inside of my mod_imp.py folder that way I don't have to clutter my main program.
import logging # Allowed me to use logging callbacks
import time # Allowed me to implement time
import random # Allowed me to use the 'random' callback
import mod_imp # Allowed me use my personal module import
# This is my logging config
logging.basicConfig(
filename="log.txt", # This either makes a file called log.txt or uses log.txt if exists
level=logging.DEBUG, # Catches errors within logging
format="%(asctime)s - %(message)s", # This tells the config what I'm needing logged
datefmt="%m-%d %H:%M" # Formats the timestamps inside the log
)
logger = logging.getLogger() # Used to record the logging messages
# This is the loop for the Magic 8 Ball
while True: # While the loop is true it will continue
ask = input("Ask a question or say 'exit' or 'bye' to end: ") # Takes user input
response = random.choice(mod_imp.magicball_all()) # Uses responses from mod_imp and gives randomly generated response
if ask == "will I become rich": # Instance of asking a different question // Easter Egg
logger.info("User asked 'will I become rich' and Magic 8 ball replied 'Don't ask questions you don't want the answer to'") # Logs special response
print("Don't ask questions you don't want the answer to")
elif ask == "": # Another easter egg
logger.info("User asked '' Magic 8 ball replied 'Sorry, mind reading is not in my capabilities. Please try again'") # Logs special response
print("Sorry, mind reading is not in my capabilites. Please try again")
elif ask.lower() == "exit" or ask.lower() == "bye": # Tells loop that if 'exit' or 'bye' is enterd in lowercase to end program
logger.info("User ended program! Magic 8 ball replied 'Thanks for playing! Goodbye!") # Sends info to log that you ended program
print("Thanks for playing! Goodbye")
break # Breaks out of loop
else:
logger.info(f"user asked: '{ask}' Magic 8 ball replied: '{response}'") # This sends all other information to log that doesn't have special condiitons
print(response) # Prints the random response from mod_impI have the following code.
The aim is to remove all special characters from the column of a DataFrame, although it does not matter if all special characters are removed from the DataFrame.
The code i have used is:
words = combined_body_title.title_body.str.split().explode().str.replace("[?.',)(/:!]","", regex=True)
This works until i put in quotation markets or brackets.
I have read the documentation, from that i think i am using it wrong. I should not be trying to change more than one character within the str.replace, but for some reason it still works just not for brackets and quotation marks.
If you could suggest an alternative solution or help me fix this one i would really appreciate it!
This can be done without regex:
>>> string = "Special $#! characters spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'
You can use str.isalnum:
S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.
If you insist on using regex, other solutions will do fine. However note that if it can be done without using a regular expression, that's the best way to go about it.
Here is a regex to match a string of characters that are not a letters or numbers:
[^A-Za-z0-9]+
Here is the Python command to do a regex substitution:
re.sub('[^A-Za-z0-9]+', '', mystring)
Your ''.join() expression is filtering, removing anything non-ASCII; you could use a conditional expression instead:
return ''.join([i if ord(i) < 128 else ' ' for i in text])
This handles characters one by one and would still use one space per character replaced.
Your regular expression should just replace consecutive non-ASCII characters with a space:
re.sub(r'[^\x00-\x7F]+',' ', text)
Note the + there.
For you the get the most alike representation of your original string I recommend the unidecode module:
Python 2
from unidecode import unidecode
def remove_non_ascii(text):
return unidecode(unicode(text, encoding = "utf-8"))
Then you can use it in a string:
remove_non_ascii("Ceñía")
Cenia
Python 3
from unidecode import unidecode
unidecode("Ceñía")