Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Member-only story

Validating Phone Number with Python

--

This story helps you to go through some easy ways to validate a phone number.

SOURCE

You might have already tried out using regex for extracting phone number for your NLP task. And like me you understood that some non-valid phone numbers can trouble you while extracting phone numbers.

Here are some ways that can help you extract phone numbers from an unstructured text file.

  1. Free API from — NUMVERIFY

This provides you a free api which help you to check and validate with an api response in json format.

import requestsresp = requests.get ('http://apilayer.net/api/validate?access_key=<YOUR ACCESS KEY> &number=+919400434933&format=1')
print(resp.json())

The response is like this:

{'valid': True, 'number': '919400434933', 'local_format': '09400434933', 'international_format': '+919400434933', 'country_prefix': '+91', 'country_code': 'IN', 'country_name': 'India (Republic of)', 'location': 'Kerala', 'carrier': 'Bharat Sanchar Nigam Ltd (BSNL)', 'line_type': 'mobile'}

2. Python library for validating phone number — phonenumbers

The main object that the library deals with is a PhoneNumber object. You can create this from a string representing a phone number using the…

--

--

Wired Wisdom
Wired Wisdom

Written by Wired Wisdom

Keep it Simple, but Significant.

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com