Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

how to remove accents from a string in python

i recently made a test in university and the question was like this-"ask a name,and remove the accents from it and print it"(there was more to the question but this was the main problem for me). My teacher didn′t allow us to use string or list methods, just the basic things literally(if, while loop, for loop, arrays),the only methods that we where allowed to use were the find() , index() and len().

So for example the name José needed to be printed has Jose, or José Magalh?es should be Jose Magalhaes

If you could say a way to to this with the basic things i would be appreciated. thank you.

question from:https://stackoverflow.com/questions/65833714/how-to-remove-accents-from-a-string-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
text_normalizing_map = {'à': 'A',
        'á': 'A',
        '?': 'A',
        '?': 'A',
        '?': 'A',
        'è': 'E',
        'é': 'E',
        'ê': 'E',
        '?': 'E',
        'í': 'I',
        'ì': 'I',
        '?': 'I',
        '?': 'I',
        'ù': 'U',
        'ú': 'U',
        '?': 'U',
        'ü': 'U',
        'ò': 'O',
        'ó': 'O',
        '?': 'O',
        '?': 'O',
        '?': 'O',
        '?': 'N',
        '?': 'C',
        'a': 'A',
        'o': 'O',
        '§': 'S',
        '3': '3',
        '2': '2',
        '1': '1',
        'à': 'a',
        'á': 'a',
        'a': 'a',
        '?': 'a',
        '?': 'a',
        'è': 'e',
        'é': 'e',
        'ê': 'e',
        '?': 'e',
        'í': 'i',
        'ì': 'i',
        '?': 'i',
        '?': 'i',
        'ù': 'u',
        'ú': 'u',
        '?': 'u',
        'ü': 'u',
        'ò': 'o',
        'ó': 'o',
        '?': 'o',
        '?': 'o',
        '?': 'o',
        '?': 'n',
        '?': 'c'}

def normalize(text):
    list_text = list(text)
    for index, i in enumerate(list_text):
        val = text_normalizing_map.get(i)
        if(val):
            list_text[index] = val
    return "".join(list_text)

print(normalize("José Magalh?es "))

output

Jose Magalhaes 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...