I have a question about removing special characters from Python.

Asked 2 years ago, Updated 2 years ago, 54 views

pattern='[\W]'

text = re.sub(pattern, ' ', text)

I don't want to remove the word 'as in isn'taren't it's, but what can I do?ㅠ<

python re.sub

2022-09-22 20:14

1 Answers

I don't think regular expressions can solve it.

import re
text = "fd-sf+1%2'fd4't&y'y67"
text2 = ''
pattern='[\W]'

for i in range(len(text)):
    if re.match("'",text[i]):
        text2 = text2 + text[i]
        continue
    text2 = text2 + re.sub(pattern, ' ', text[i])
>>> text2
"fd sf 1 2'fd4't y'y67"


2022-09-22 20:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.