Lucas Wetherall
Lucas Wetherall

Reputation: 21

Python Valid Phone numbers

So im trying to read phone numbers from a file but i cant get it to handle numbers if I add extra numbers to the end EX: (123) 456-7890 is good but (123) 456-7890123 also goes through. How can I check for extra numbers at the end.

import re # Import Real Expressions

def isValid(s):

    Filter1 = re.compile("[0-9]{3}\-[0-9]{3}\-[0-9]{4}") #Test for format xxx-xxx-xxxx
    return Filter1.match(s) #return true if matches format

def isValid2(s):
    Filter2 = re.compile("\([0-9]{3}\) [0-9]{3}\-[0-9]{4}") #Test for format (xxx) xxx-xxxx
    return Filter2.match(s)# return true if matches format

def findValidPhone():
    filename = "input1.txt" #delcare filename
    with open(filename,"r") as inFile: #openfile
        for line in inFile: #for all the lines in the file
            s = line # store the line as a variable
            # print(s)
            if ( isValid(s)): #run tests using function isValid if true print number
                print(s)
            elif(isValid2(s)): #run test using function isValid2 if true print number
                print(s)
            else: # print invalid number if an invalid number is found in the file
                print("Invalid Number")
inFile.close() #close the file
findValidPhone() #function call

Upvotes: 1

Views: 9537

Answers (2)

victoria55
victoria55

Reputation: 245

You can use the function validate_phone() from the library DataPrep. Install it with pip install dataprep.

>>> from dataprep.clean import validate_phone
>>> df = pd.DataFrame({'phone': ['(123) 456-7890', '(123) 456-7890123']})
>>> validate_phone(df['phone'])
0     True
1    False
Name: phone, dtype: bool

Upvotes: 0

Duncan
Duncan

Reputation: 95742

You can use the phonenumbers library to test whether you have a valid phone number. Install it with pip install phonenumbers.

You can parse individual number strings and test them for validity:

>>> import phonenumbers
>>> print(phonenumbers.parse("(541) 754-3010", "US"))
Country Code: 1 National Number: 5417543010
>>> phonenumbers.is_valid_number(phonenumbers.parse("(541) 754-3010", "US"))
True

It will do a lot more checking than your regular expression as apparently neither of your examples is a valid US phone number:

>>> phonenumbers.is_valid_number(phonenumbers.parse("(123) 456-7890123", "US"))
False
>>> phonenumbers.is_valid_number(phonenumbers.parse("(123) 456-7890", "US"))
False

Extract numbers from a larger text block:

>>> text = '''So im trying to read phone numbers from a file but
... i cant get it to handle numbers if I add extra numbers to the
... end EX: (123) 456-7890 is good but (123) 456-7890123 also goes
... through. How can I check for extra numbers at the end.
... Also we can try (541) 754-3010 as a possible number.
... '''
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...    print(match.number)
...
Country Code: 1 National Number: 5417543010
>>>
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...    print(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
...    print(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.NATIONAL))
...
+1 541-754-3010
(541) 754-3010

See https://github.com/daviddrysdale/python-phonenumbers for more about this library.

Upvotes: 2

Related Questions