utils库中Django的phone2numeric方法的目的是什么?

时间:2019-04-08 20:08:45

标签: python django function

Django具有以下定义的以下功能phone2numeric(phone)

def phone2numeric(phone):
    """Convert a phone number with letters into its numeric equivalent."""
    char2number = {
        'a': '2', 'b': '2', 'c': '2', 'd': '3', 'e': '3', 'f': '3', 'g': '4',
        'h': '4', 'i': '4', 'j': '5', 'k': '5', 'l': '5', 'm': '6', 'n': '6',
        'o': '6', 'p': '7', 'q': '7', 'r': '7', 's': '7', 't': '8', 'u': '8',
        'v': '8', 'w': '9', 'x': '9', 'y': '9', 'z': '9',
    }
    return ''.join(char2number.get(c, c) for c in phone.lower())

从外观上看,它基本上需要一个数字,例如1-800 GALAXY并将GALAXY部分更改为425299,因此它变为1-800425299。我只是想知道为什么Django实现了这样的特定功能?

1 个答案:

答案 0 :(得分:-1)

似乎将电话号码的文本从移动版式转换为真实电话号码。下面是示例布局和演示。

[![In \[14\]: def phone2numeric(phone):
    ...:         """Converts a phone number with letters into its numeric equivalent."""
    ...:         char2number = {
    ...:             'a': '2', 'b': '2', 'c': '2', 'd': '3', 'e': '3', 'f': '3', 'g': '4',
    ...:             'h': '4', 'i': '4', 'j': '5', 'k': '5', 'l': '5', 'm': '6', 'n': '6',
    ...:             'o': '6', 'p': '7', 'q': '7', 'r': '7', 's': '7', 't': '8', 'u': '8',
    ...:             'v': '8', 'w': '9', 'x': '9', 'y': '9', 'z': '9',
    ...:         }
    ...:         return ''.join(char2number.get(c, c) for c in phone.lower())
    ...: 

In \[15\]: phone = "ghab3wmded"

In \[16\]: phone2numeric(phone)
Out\[16\]: '4422396333'

In \[17\]:][1]][1]