user4279562
user4279562

Reputation: 669

convert an encoding to just alphabetical expression

I have a city name string such as

a='A Coruña'

How can I convert this into 'A Coruna'?

Best

Upvotes: 0

Views: 52

Answers (2)

Parul Dixit
Parul Dixit

Reputation: 364

for python 3.x

>>>import unicodedata
>>>a = 'A Coruña'
>>>search_string = ''.join((c for c in unicodedata.normalize('NFD', a)if unicodedata.category(c) != 'Mn'))
>>>print(search_string)
>>>A Coruna

Upvotes: 2

bouteillebleu
bouteillebleu

Reputation: 2493

The Unidecode module looks like it should do what you want.

Install it with:

pip install Unidecode

Python 3

In Python 3 it's pretty simple:

>>> from unidecode import unidecode
>>> a='A Coruña'
>>> unidecode(a)
'A Coruna'

Python 2

With Python 2, you'll need to either define your string as a Unicode string from the beginning:

a = u'A Coruña'

Or, if you've already got it as a string, you'll need to convert it to a Unicode string using .decode():

a = 'A Coruña'.decode('utf-8')

And then you can run unidecode(a) as in the example for Python 3.

Upvotes: 2

Related Questions