Member-only story
Validating Phone Number with Python
This story helps you to go through some easy ways to validate a phone number.
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.
- 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
PhoneNumberobject. You can create this from a string representing a phone number using the…





![How to Create & Structure a Complex Tkinter Application [2022]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*hF7TsinyHGev8B1vNeaxiQ.jpeg)




