Python有效电话号码

时间:2018-09-26 14:48:42

标签: python phone-number

所以我试图从文件中读取电话号码,但是如果我在末尾添加额外的号码,我将无法处理电话号码  例:(123)456-7890很好,但(123)456-7890123也可以通过。我该如何在最后检查额外的数字。

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

2 个答案:

答案 0 :(得分:2)

您可以使用phonenumbers库来测试您是否具有有效的电话号码。使用pip install phonenumbers安装它。

您可以解析单个数字字符串并测试其有效性:

>>> 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

它将比您的正则表达式执行更多检查,因为显然您的示例都不是有效的美国电话号码:

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

从较大的文本块中提取数字:

>>> 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

有关此库的更多信息,请参见https://github.com/daviddrysdale/python-phonenumbers

答案 1 :(得分:0)

您可以使用库 validate_phone() 中的函数 DataPrep。使用 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