【问题标题】:extracting unchanged raw phone numbers with phonenumbers.PhoneNumberMatcher使用 phonenumbers.PhoneNumberMatcher 提取未更改的原始电话号码
【发布时间】:2021-02-24 14:20:54
【问题描述】:

我正在尝试从文本中提取电话号码。但我不希望重新格式化电话、提取数字部分或其他任何东西,但我只想从文本中提取未更改的、不重新格式化的原始电话号码字符串。 我用 PhoneNumberMatcher() 方法尝试了“phonenumbers”库(https://github.com/daviddrysdale/python-phonenumbers)。它工作得很好,就获取格式化的电话号码而言,这是我不需要的,我只需要电话号码的原始子字符串。有办法吗?

for match in phonenumbers.PhoneNumberMatcher(text, "US"):
    unformatted_phone = match.number
    #formatted_phone = str(match.number)
    #formatted_phone = phonenumbers.format_number(match.number, None)
    formatted_phone = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

【问题讨论】:

    标签: python regex phone-number data-extraction


    【解决方案1】:

    好的,我知道了,它是 .raw_string

    phones = []
    for match in phonenumbers.PhoneNumberMatcher(text, "US"):
        phone = match.raw_string
        phones.append(phone)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2015-08-09
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多