Skip to main content

Phonenumber to Country (ISO 3166-1) mapping

Project description

Small project to map an E.164 (international) phone number to the ISO-3166-1 alpha 2 (two letter) country code, associated with that number.

Also provides mapping for E.212 (mobile network codes, mcc+mnc) to the country.

The package has no dependencies, and works on Py2.7+, Py3 and PyPy.

Simple Usage

To simply get a country code from a phone number or mcc, mnc.

>>> from phone_iso3166.country import *
>>> phone_country(45)
'DK'
>>> phone_country('+1 202-456-1111')
'US'
>>> phone_country(14412921234)
'BM'
>>> network_country(238, 1)
'DK'

Network names and codes

If you want more detailed information about the networks, such as the operator name use the functions in phone_iso3166.network.

>>> from phone_iso3166.network import *
>>> network(238, 1)
('DK', 'TDC Mobil')
>>> country_networks('DK')
[(238, 1, 'TDC Mobil'), (238, 2, 'Telenor'), (238, 3, 'MIGway A/S'),
 (238, 4, 'NextGen Mobile Ltd T/A CardBoardFish'), (238, 6, 'Hi3G'),
 (238, 8, 'Nordisk Mobiltelefon Danmark A/S'), (238, 10, 'TDC Mobil'),
 (238, 43, 'MobiWeb Limited'), (238, 12, 'Lycamobile Denmark'),
 (238, 13, 'Compatel Limited'), (238, 77, 'Tele2'), (238, 20, 'Telia'),
 (238, 66, 'TT-Netvaerket P/S'), (238, 28, 'CoolTEL'),
 (238, 30, 'Interactive Digital Media GmbH')]

More information

If want more information, you can easily use the country code with other python packages such as pycountry.

>>> from phone_iso3166.country import phone_country
>>> import pycountry
>>> phone = '+55 21 3814-2121'
>>> c = pycountry.countries.get(alpha_2=phone_country(phone))
>>> c.name
'Brazil'
>>> c.official_name
'Federative Republic of Brazil'

This package makes no attempt to understand the various input options for phone numbers, and assumes an international phone number. If you deal in fuzzy inputs, try phonenumbers.

>>> from phone_iso3166.country import phone_country
>>> import phonenumbers
>>> import pycountry
>>> local = phonenumbers.parse("020 8366 1177", "GB")
>>> phonenumbers.format_number(local, phonenumbers.PhoneNumberFormat.E164)
'+442083661177'
>>> cc = phone_country(str(local.country_code)+str(local.national_number))
>>> uk = pycountry.countries.get(alpha_2=cc)
>>> uk.name
'United Kingdom'
>>> uk.official_name
'United Kingdom of Great Britain and Northern Ireland'
>>> wh = phonenumbers.parse("0012024561111", "GB")
>>> cc_wh = phone_country(str(wh.country_code)+str(wh.national_number))
>>> cc_wh
'US'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

phone-iso3166-0.3.3.tar.gz (34.1 kB view details)

Uploaded Source

Built Distributions

phone_iso3166-0.3.3-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

phone_iso3166-0.3.3-py2.py3-none-any.whl (34.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file phone-iso3166-0.3.3.tar.gz.

File metadata

  • Download URL: phone-iso3166-0.3.3.tar.gz
  • Upload date:
  • Size: 34.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for phone-iso3166-0.3.3.tar.gz
Algorithm Hash digest
SHA256 796e6636710189c6e104d2111fbbf903f9ca54942abdbb1d101c0a22b3c7384a
MD5 df1f4fad70914034e78dc1b8941eeff1
BLAKE2b-256 83120ed405ce5e411c8eb5a86147e4c3da10429ea029c8285efe0a88c1200f1a

See more details on using hashes here.

File details

Details for the file phone_iso3166-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for phone_iso3166-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e1fd8f63e81d4ec7072f6560cf24c3fef45a5f1638e3c843f0fb724fd6ceda5
MD5 086c4cc2207bd6a6abc8ccbc0ed49882
BLAKE2b-256 77f4f6ce1ed148566a9f67456f7704b9b815acf5e69b8b420b0c70906296faa8

See more details on using hashes here.

File details

Details for the file phone_iso3166-0.3.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for phone_iso3166-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66630f8204e6dbb965eb536ce639c3c3a6a61709ec26a9877a28097b05e5410c
MD5 e7ca9c66aed5271c8cd1a765045c90d1
BLAKE2b-256 9345fc00f71f64edc4cce8a5d5b82ea50b7a1005b125c58b74e28d9cf9c4c22a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page