5

I have a thousand of phone numbers like +27823374587

Is there a library or command-line program that can tell me the country?

For instance: +27823374587South Africa

Requirements:

  • Runs offline
  • Gratis, ideally open source
  • Any OS or programming language is fine

2 Answers 2

4

There are a couple of python libraries that can do this python-phonenumbers will parse various formats to get the country code.

  • Free & Open Source.
  • Works offline
  • Cross platform & OS
  • Python 2.5-2.7 & 3.x
  • Pure Python so no C compiler needed.

Based on the examples from that page and using ipython:

In [1]: import phonenumbers

In [2]: from phonenumbers import geocoder

In [3]: query = phonenumbers.parse("+27823374587", None)

In [4]: print repr(geocoder.description_for_number(query, "en"))

u'South Africa'

2
  • 2
    Nice library, it seems to do 90% of the job: "+27823374587"→"+27". I guess I could write mapping code to make it "+27823374587"→"South Africa" but if there is already a library that does the whole thing it would be great :-)
    – Nicolas Raoul
    Commented Jul 11, 2014 at 13:39
  • @NicolasRaoul - It does - I have added an example to the answer above. (There was one on the page but I guess it was lost in the details). Commented Jul 12, 2014 at 5:22
2

You can ofcourse use pure-perl library too.

Here is an working script:

#!/usr/bin/env perl

use 5.014;
use warnings;
use Number::Phone::Country;
use Locale::Country;

for my $arg (@ARGV) {

    #get ISO country code and IDD from the number
    my($iso_cc, $idd) = Number::Phone::Country::phone2country_and_idd($arg);
    unless($iso_cc) {
        warn "Bad phone number $arg";
        next;
    }

    say "Phone: $arg";
    say "ISO Code: $iso_cc";
    say "International direct dialing: $idd";

    #get the country name from the ISO code
    my $country = code2country($iso_cc); 
    say "Country name: $country\n";
}

If you run it as:

perl scriptname "+1 302 1234567" +421905905905

outputs:

Phone: +1 302 1234567
ISO Code: US
International direct dialing: 1
Country name: United States

Phone: +421905905905
ISO Code: SK
International direct dialing: 421
Country name: Slovakia

The script uses two perl modules:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.