【问题标题】:How to get the country code and national phone number after parsing using the phonenumbers package?使用 phonenumbers 包解析后如何获取国家代码和国家电话号码?
【发布时间】:2019-07-29 09:56:30
【问题描述】:
我正在申请清除手机号码。我正在使用电话号码包。所以我用了phonenumbers.parse(Cell No, Country Initials)
在控制台上看起来像这样:
国家代码:### 国家号码:###
我原本打算只删除一些文本以获取区号和国家号码,但我记得区号和国家号码字符长度在其他国家/地区不同。
有没有办法单独获取区号和国家号码
【问题讨论】:
标签:
python
package
phone-number
【解决方案1】:
phonenumbers.parse 返回一个PhoneNumber 对象。
在y = phonenumbers.parse("020 8366 1177", "GB") 之后,您可以通过例如访问属性y.country_code 或通过y.country_code = 49 等设置它们。
区号的提取有点棘手,因为并非所有国家都有区号的概念。如何正确分别获取区号和国内用户号码见this code snippet。