Created
May 5, 2017 11:03
-
-
Save maxpoletaev/40651073c766896e0120b017c9050b62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment