在搜索表单中删除重音符号/变音符号

时间:2019-06-26 23:43:24

标签: python python-3.x

我不知道放在哪里

import unicodedata

unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')

这里

编辑:

我应该立即粘贴所有代码。

from BTCInput import *

class Contact:
    pass

contacts = []

def new_contact():
    print('new contact')
    new_contact = Contact()
    new_contact.name = read_text('name: ')
    new_contact.address = read_text('address: ')
    new_contact.telephone = read_text('phone: ')
    contacts.append(new_contact)

def find_contact():
    print('find contact')
    search_name = read_text('name: ')
    search_name = search_name.strip()
    search_name = search_name.lower()
    result = None
    for contact in contacts:
        name = contact.name
        name = name.strip()
        name = name.lower()
        if name.startswith(search_name):
            result = contact
            break

    if result != None:
        print(f'name: {result.name}\n'
              f'Address: {result.address}\n'
              f'phone: {result.telephone}\n')
    else:
        print('xyz')

while True:
    menu = int(input('name'))
    if menu == 1:
        new_contact()
    elif menu == 2:
        find_contact()

所以...我想从搜索表单中忽略变音符号。 我不想更改代码,因为这是我实际上正在阅读的书中的一个示例。我只想改善它。

1 个答案:

答案 0 :(得分:0)

尝试此代码:

from unidecode import unidecode
def find_contact(contacts):
    print('Find contact')
    search_name = input('Enter the name: ')
    search_name = search_name.strip()
    search_name = search_name.lower()
    search_name = unidecode(search_name)
    all_contacts_name=[]
    for key,values in contacts.items():
        for contact in values:
            name = contact["name"]
            name = name.strip()
            name = name.lower()
            all_contacts_name.append(name)

    if search_name in all_contacts_name:
        print("this contact exists")

    else:
        print("this contact does not exisit")

find_contact({"contacts":[{"name":"xxxx"},{"name":"ZOLC"}]})

输出:

Enter the name: ŻÓŁĆ
this contact exists