【问题标题】:get a phone number's international prefix from a country code in python从python中的国家代码获取电话号码的国际前缀
【发布时间】:2015-06-27 19:23:48
【问题描述】:

是否可以使用python-phonenumbers 或其他 python 库从两个字母的国家/地区代码 (ISO 3166-1 alpha-2) 中获取国家/地区呼叫代码?

phonenumbers 库中的示例侧重于从数字中提取国家/地区代码,但我想做相反的事情,例如:

"US" -> "1" "GB" -> "44" "CL" -> "56"

【问题讨论】:

标签: python internationalization phone-number


【解决方案1】:

phonenumbers 库实际上具有(至少从 8.10.5 版开始)country_code_for_region() 函数:

>>> import phonenumbers
>>> phonenumbers.country_code_for_region("GB")
44

【讨论】:

    【解决方案2】:

    使用库。

    In [1]: from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
    
    In [2]: COUNTRY_CODE_TO_REGION_CODE
    Out[2]: 
    {1: ('US',
         'AG',
         'AI',
    
    ....
     7: ('RU', 'KZ'),
     20: ('EG',),
     27: ('ZA',),
     30: ('GR',),
     31: ('NL',),
     32: ('BE',),
     33: ('FR',),
     34: ('ES',),
     36: ('HU',),
     39: ('IT', 'VA'),
     40: ('RO',),
     ... snip.
    

    最终:

    from phonenumbers import COUNTRY_CODE_TO_REGION_CODE, REGION_CODE_FOR_NON_GEO_ENTITY
    REGION_CODE_TO_COUNTRY_CODE = {}
    
    for country_code, region_codes in COUNTRY_CODE_TO_REGION_CODE.items():
        for region_code in region_codes:
            if region_code == REGION_CODE_FOR_NON_GEO_ENTITY:
                continue
            if region_code in REGION_CODE_TO_COUNTRY_CODE:
                raise ValueError("%r is already in the country code list" % region_code)
            REGION_CODE_TO_COUNTRY_CODE[region_code] = str(country_code)
    

    以下函数将为您提供来自提供的 iso 代码的调用代码:

    def get_calling_code(iso):
      for code, isos in COUNTRY_CODE_TO_REGION_CODE.items():
        if iso.upper() in isos:
            return code
      return None
    

    这给了你:

    get_calling_code('US')
    >> 1
    get_calling_code('GB')
    >> 44
    

    【讨论】:

      【解决方案3】:

      使用python-phonenumbers,您可以利用 COUNTRY_CODE_TO_REGION_CODE 映射,它是一个以国际呼叫代码 (int) 作为键,国家代码 (str) 作为值的字典。您只需反转 dict 即可完成工作。
      这里是一个例子(非常类似于toast38cozacgte的答案):

      REGION_CODE_TO_COUNTRY_CODE = {}
      for k, vs in phonenumbers.COUNTRY_CODE_TO_REGION_CODE.items(): # prefix -> country code: 39 -> 'IT'
          for v in vs:   #because a prefix could belong to more countries
             REGION_CODE_TO_COUNTRY_CODE[v] = k # country code-> prefix : 'IT' -> 39# now you have your reversed map
      
      print( 'Italy country prefix: +'+ str( REGION_CODE_TO_COUNTRY_CODE['IT'] ) )
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        我不知道任何用于此的 python 库,但 here'sa csv 具有所有 ISO 3166-1 alpha-2 代码及其数字前缀,从那里查找它应该是微不足道的:

        import csv
        
        country_to_prefix = {}
        
        with open("countrylist.csv") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                country_to_prefix[row["ISO 3166-1 2 Letter Code"]] = row["ITU-T Telephone Code"]
        
        print country_to_prefix["US"] # +1
        print country_to_prefix["GB"] # +44
        print country_to_prefix["CL"] # +56
        

        编辑:上面的链接失效了,但是我在 Github 上找到了repository with that data (and more)

        【讨论】:

        • 该链接不再起作用,因为该域无法再解析。
        • @Anthon 感谢您的提醒,我添加了指向新源的链接(但尚未更新代码)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        相关资源
        最近更新 更多