Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Created May 5, 2017 11:03
Show Gist options
  • Save maxpoletaev/40651073c766896e0120b017c9050b62 to your computer and use it in GitHub Desktop.
Save maxpoletaev/40651073c766896e0120b017c9050b62 to your computer and use it in GitHub Desktop.
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