Skip to content Skip to sidebar Skip to footer

Maintaining The Consistency Of Strings Before And After Converting To Ascii

I have many strings in unicode format such as carbon copolymers—III\n12- Géotechnique\n and many more having many different unicode characters, in a string variable named txtWor

Solution 1:

Use the \w regular expression to strip non-alphanumerics before the decomposing trick:

#coding:utf8from __future__ import unicode_literals,print_function
import unicodedata as ud
import re
txtWords = 'carbon copolymers—III\n12- Géotechnique\n'
txtWords = re.sub(r'[^\w\n]',r' ',txtWords.lower(),flags=re.U)
txtWords = ud.normalize('NFKD',txtWords).encode('ascii','ignore').decode()
print(txtWords)

Output (Python 2 and 3):

carbon copolymers iii
12  geotechnique

Post a Comment for "Maintaining The Consistency Of Strings Before And After Converting To Ascii"