Created
February 16, 2013 02:31
-
-
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
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 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