How to format phone numbers to E.164 and tell mobile from landline in bulk
E.164 is the format phone systems and messaging services expect: a plus sign, the country code and the number, digits only, at most 15 digits. (415) 555-2671 in the United States becomes +14155552671. 020 7946 0958 in the United Kingdom becomes +442079460958.
Why find-and-replace is not enough
The rules differ by country. A British number drops its leading zero when the country code is added; an Italian landline keeps it (06 1234 5678 becomes +390612345678). Number lengths differ, some ranges are not in use, and a number written without a country code means nothing until you know which country it was written in.
Google publishes these rules as an open-source library, libphonenumber, with versions for Java, JavaScript, Python, Go, PHP and other languages. It is free and runs on your own computer.
A script for a list
With Python installed, run pip install phonenumbers, put one number per line in numbers.txt and save this as format.py:
import csv, sys
import phonenumbers
from phonenumbers import PhoneNumberFormat, PhoneNumberType, number_type
DEFAULT_COUNTRY = "US" # used only for numbers written without a +
NAMES = {PhoneNumberType.MOBILE: "mobile", PhoneNumberType.FIXED_LINE: "fixed_line",
PhoneNumberType.FIXED_LINE_OR_MOBILE: "fixed_line_or_mobile",
PhoneNumberType.VOIP: "voip", PhoneNumberType.TOLL_FREE: "toll_free",
PhoneNumberType.PREMIUM_RATE: "premium_rate"}
out = csv.writer(sys.stdout)
out.writerow(["input", "valid", "e164", "type"])
for line in open("numbers.txt", encoding="utf-8"):
raw = line.strip()
if not raw:
continue
try:
number = phonenumbers.parse(raw, DEFAULT_COUNTRY)
except phonenumbers.NumberParseException:
out.writerow([raw, False, "", ""])
continue
if not phonenumbers.is_valid_number(number):
out.writerow([raw, False, "", ""])
continue
out.writerow([raw, True,
phonenumbers.format_number(number, PhoneNumberFormat.E164),
NAMES.get(number_type(number), "other")])
python format.py > formatted.csv
Open formatted.csv in a spreadsheet. Store the e164 column; show people the number in their local format.
Mobile or landline
The type column comes from the numbering plan: which ranges a country has set aside for mobile, fixed lines, internet telephony, toll-free and premium numbers. Use it to keep landlines out of a text-message campaign.
Know its limits:
- In the United States, Canada and some other countries, mobile and fixed numbers share the same ranges. The answer there is
fixed_line_or_mobile, and no numbering plan can do better. - The plan says a number is correctly formed and belongs to a range in use. It does not say the line is in service, or who owns it.
- A number that was moved to another carrier still looks like its original range.
Telling whether a line is live, or splitting mobile from landline in the United States, needs a carrier lookup service that is paid per number.
Our Phone Number Validator on Apify does the same for a pasted list of up to 200,000 numbers and adds the country, the location for geographic numbers, the time zones and the original carrier where it is published. It makes no calls and sends no texts. It costs $0.002 a number, paid by card through Apify, with no subscription.