def normalise_phone_number(nr): if not nr: return nr try: number = pn_parse(nr, 'CH') return pn_format_number(number, PhoneNumberFormat.E164) except NumberParseException: # Apparently, not all numbers are valid. # e.g. 'faker' (used in sample data) produces phone numbers such as # u'+08(2)9338238082'. In such cases, we try our own version: nr = nr.replace('(', '').replace(')', '').replace(' ', '') if nr.startswith('+'): return nr if nr.startswith('00'): return nr.replace('00', '+', 1) raise ValueError("Invalid number")
def normalize_phone_number(num): return pn_format_number(pn_parse(num, 'US'), PhoneNumberFormat.INTERNATIONAL)