Skip to content

Instantly share code, notes, and snippets.

@TMcManus
Created February 16, 2013 02:31
Show Gist options
  • Save TMcManus/4965227 to your computer and use it in GitHub Desktop.
Save TMcManus/4965227 to your computer and use it in GitHub Desktop.
Example of a python function for normalizing Japanese local phone number formats to E.164 using the incredible python port of Google's libphonenumber project: https://github.com/daviddrysdale/python-phonenumbers
import phonenumbers as ph
def japan_format(phone_number, country_code='JP'):
"""
Converts local Japanese formatted number to E.164 format
Arguments:
phone_number -- A Japanese phone number in any format as a string
>>> japan_format("080-1234-5678")
u'+818012345678'
>>> japan_format("03.1234.5678")
u'+81312345678'
>>> japan_format("9012345678")
u'+819012345678'
>>> japan_format("+81 70 1234 5678")
u'+817012345678'
"""
number_object = ph.parse(phone_number, region=country_code)
return ph.format_number(number_object, ph.PhoneNumberFormat.E164)
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment