从python中的国家/地区代码获取电话号码的国际前缀 [英] get a phone number's international prefix from a country code in python
问题描述
是否可以使用 python-phonenumbers 或其他python库来获取调用代码的国家/地区来自两个字母的国家/地区代码( ISO 3166-1 alpha-2 )?
Is it possible to use python-phonenumbers or another python lib to get a country calling code from a two letter country code (ISO 3166-1 alpha-2)?
phonenumbers
lib中的示例着重于从数字中提取国家/地区代码,但我想做相反的事情,例如:
The examples in the phonenumbers
lib focus on extracting a country code from a number, but I'd like to do the opposite, something like:
"US" -> "1"
"GB" -> "44"
"CL" -> "56"
推荐答案
对此我不知道有任何python库,但是此处是一个包含所有ISO 3166-1 alpha-2代码及其数字前缀的csv,从那里查找它应该很简单:
I don't know of any python lib for this, but here's a csv with all ISO 3166-1 alpha-2 codes and their number prefixes, it should be trivial to look it up from there:
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上使用这些数据(以及更多).
edit: The above link has gone down, but I've found a repository with that data (and more) on Github.
这篇关于从python中的国家/地区代码获取电话号码的国际前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!