Why doesn't the python phonenumbers library work in this case?

It seems that '5187621769' should be very simple for the phonenumbers library for parsing. These are 10 digits with a country code in the USA. But ... no luck.

Setup:

import phonenumbers
number = '5187621769'

Method 1:

parsed = phonenumbers.parse(number)

This causes an error.

Method 2:

parsed = phonenumbers.parse("+" + number)

Gives the country code = 51, which is not the United States.

I know that I can:

parsed = phonenumbers.parse(number,region="US")

But I do not always know that the number will be US (this is just one case when I found that I did not get the desired behavior). Is there an option or trick for formatting that I skip? Thank!

+4
source share
3 answers

You should just use:

parsed = phonenumbers.parse(number, 'US')
+4
source

, phonenumbers . "+1" a.k.a. , , phonenumbers , - , - :

try:
    parsed = phonenumbers.parse(number)
except phonenumbers.NumberParseException as npe:
    parsed = phonenumbers.parse('+1{}'.format(number))
+3

When searching for phone numbers, you need to write a country code with a phone number.

Also, here is the API (libphonenumber shell) for phone numbers:

http://blog.ones-app.com/ones-phone-number-api/

+1
source

All Articles