Alireza
Alireza

Reputation: 6848

Why python Phonenumbers library give null for country of +358*****?

I use the below code to get the country of phone number and I currently use phonenumbers:

query = phonenumbers.parse(number, None)
geocoder.country_name_for_number(query, "en")

It gives error for numbers with +358. The library version I tested was the latest: 7.4.4

Anyone has a solution or workaround with this library? The country should be Finland


EDIT1: For example the below number gives an empty string:

>>> number = '+358753263000'
>>> query = phonenumbers.parse(number, None)
>>> geocoder.country_name_for_number(query, "en")
u''

Upvotes: 3

Views: 621

Answers (3)

Alireza
Alireza

Reputation: 6848

I have opened an issue on their github page:

Why python phonenumbers library return empty string for country of +3587*****?

In case there was an answer, I would post it here later on.

Upvotes: 0

aneroid
aneroid

Reputation: 15962

You may have overmasked it. As @Barmar pointed out, according to Wikipedia, the numbers after the '+358' the next number should be 9 (or 0).

>>> geocoder.country_name_for_number(phonenumbers.parse('+358753263000'), 'en')
u''
>>> geocoder.country_name_for_number(phonenumbers.parse('+358953263000'), 'en')
u'Finland'

Changing that first number from 7 to 9 gives Finland. You also have too many digits but it doesn't seem to mind.

Upvotes: 2

L3viathan
L3viathan

Reputation: 27273

I think the problem is that the prefix +358 is ambiguous. You can see that if you call

>>> geocoder.region_codes_for_country_code(358)
('FI', 'AX')

+358 is the prefix both for Finland and for the Åland Islands (AX).

If a code is ambiguous, country_name_for_number then checks all of the regions and calls is_valid_number_for_region for the specific number. If it finds that the number is valid in more than one region, it returns an empty string.

Upvotes: 1

Related Questions