Please write a function that replaces all characters that the user provides by empty spaces.
The function is:
- Is called
clean_text_general()
; - Has one positional parameter
text
; - Has one keyword parameter
chars_to_remove
, which is a set (set the default to{'\n', ',', '.', '"'}
); - Returns a string, e.g., the cleaned text
When the user provides a different value to chars_to_remove
, e.g., {'a'}
, then only those characters should be replaced by empty spaces in the text.
Here is my code so far:
def clean_text_general(text, chars_to_remove=text.replace(',', '').replace('.', '').replace('\n', '').replace('"', '')):
text = text(chars_to_remove(text))
print(text)
clean_text(a_story,chars_to_remove=text.replace(',', '').replace('.', '').replace('\n', '').replace('"', ''))
The input can be any string.
CodePudding user response:
Here is a possible solution:
def clean_text_general(text, chars_to_remove={'\n', ',', '.', '"'}):
return text.translate(text.maketrans({k: '' for k in chars_to_remove}))
Example:
>>> clean_text_general('hello, world.\n')
'hello world'
>>> clean_text_general('hello, world.\n', chars_to_remove={'\n', ','})
'hello world.'
If you don't feel comfortable using str.translate
and str.maketrans
then you can use this one (it's less efficient):
def clean_text_general(text, chars_to_remove={'\n', ',', '.', '"'}):
for c in chars_to_remove:
text = text.replace(c, '')
return text
CodePudding user response:
You could utilise the maketrans and translate string functions like this:
import string
def clean_text_general(s):
return s.translate(s.maketrans({k: ' ' for k in string.punctuation}))
print(clean_text_general('a,b.c'))
Output:
a b c