2

Say I have

text = "El próximo AÑO, vamos a salir a Curaçao... :@ :) será el día #MIÉRCOLES 30!!!!"

How can I turn it into

text2 = "El próximo AÑO vamos a salir a Curaçao será el día MIÉRCOLES 30"

Using regex?

blhsing
  • 77,832
  • 6
  • 59
  • 90
NicolasVega
  • 61
  • 1
  • 6
  • May be this can be helpful: https://stackoverflow.com/questions/1276764/stripping-everything-but-alphanumeric-chars-from-a-string-in-python – Forever Learner Oct 09 '18 at 01:19

2 Answers2

1

You can try using the \W character class:

re.sub(r'\W+', ' ', text)
Carlos Mermingas
  • 3,602
  • 2
  • 20
  • 39
0

If you need compatibility with Python 2.7 you can use the str.isalpha() method:

# -*- coding: utf-8 -*-
import re
text = u"El próximo AÑO, vamos a salir a Curaçao... :@ :) será el día #MIÉRCOLES 30!!!!"
print(re.sub(' +', ' ', ''.join(c for c in text if c.isalpha() or c.isdigit() or c.isspace())))

This outputs:

El próximo AÑO vamos a salir a Curaçao será el día MIÉRCOLES 30
blhsing
  • 77,832
  • 6
  • 59
  • 90