TAGS :Viewed: 1 - Published at: a few seconds ago

[ 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")

Answer 1

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')
>>> geocoder.country_name_for_number(phonenumbers.parse('+358953263000'), 'en')

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

Answer 2

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.

Answer 3

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.