python normalize_phone.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python normalize_phone.py相关的知识,希望对你有一定的参考价值。

import string


def normalize_phone(phone):
    if not phone:
        return None

    phone = ''.join(n for n in phone if n in set(string.digits + '+'))
    phone_len = len(phone)

    if phone_len == 12:
        if phone.startswith('380'):
            phone = '+' + phone
    elif phone_len == 11:
        if phone.startswith('8'):
            phone = '+7' + phone.lstrip('8')
        elif phone.startswith('7'):
            phone = '+' + phone
    elif phone_len == 10:
        if phone.startswith('0'):
            phone = '+38' + phone
        elif phone.startswith('9'):
            phone = '+7' + phone

    if len(phone) < 12 or len(phone) > 13 or not phone.startswith('+'):
        return None

    return phone

以上是关于python normalize_phone.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python