【问题标题】:Is there a way to add invalid phone numbers conditions in my code有没有办法在我的代码中添加无效的电话号码条件
【发布时间】:2019-08-20 08:49:01
【问题描述】:

我在 python(pandas)、databricks 中有以下代码。这工作正常,但它没有过滤掉无效的电话号码。

代码遵循模式并过滤掉家庭和手机号码

import pandas as pd 
import re
from pyspark.sql.functions import lit

df = Phonevalidation

# function to check the phone number pattern
def isValid(s): 
  Pattern = re.compile("(0|44)?[7-9][0-9]{9}") 
  if(Pattern.match(s)):
    return 'Mobile Number'
  else: return 'Home phone'

#UDF Register
PhType = udf(isValid)

df1 = Phonevalidation.withColumn('Phtype' ,PhType('Phonenumber') )
display(df1)

我希望过滤掉长度 >10 或

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

    您当前使用的代码带有 9 位数字和前导零或英国国家代码,然后是初始 7、8 或 9 作为手机号码,但其他所有内容(包括格式错误的号码)作为家庭号码:

      Pattern = re.compile("(0|44)?[7-9][0-9]{9}") 
      if(Pattern.match(s)):
        return 'Mobile Number'
      else: return 'Home phone'
    

    如果您关注美国号码,grep with regex for phone number 可能会有所帮助。

    我希望过滤掉长度 >10 或

    对于您的想法的第一部分,您可以使用Pattern = re.compile("[0-9]{10}") 之类的模式,第二部分我会放入类似的伪代码中

    if (Pattern.match(s)):
       if (s != '0000000000' or s != '1111111111'):
          return: 'Fitting your criteria'
    else: return 'Not valid' 
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      相关资源
      最近更新 更多