【问题标题】:Python Valid Phone numbersPython 有效电话号码
【发布时间】:2018-09-26 14:48:41
【问题描述】:

所以我试图从文件中读取电话号码,但如果我在末尾添加额外的号码,我无法让它处理号码 例如:(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

【问题讨论】:

标签: python phone-number


【解决方案1】:

您可以使用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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2018-07-07
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多