,玻利瓦尔共和国“,“VN”:“越南”,“VG”:“英属维尔京群岛”,“VI”:“美属维尔京群岛”,“WF”:“瓦利斯和富图纳群岛”,“EH”:“西方 撒哈拉 </跨度> ”“YE”:“也门”“ZM”:“赞比亚”“ZW”:“津巴布韦
如果您的输入字符集仅限于ASCII,您仍然可以使用转义序列获得所需的重音符号。试试这个:
import pycountry import pprint pprint.pprint ({country.alpha2 : country.name for country in pycountry.countries})
这会生成如下所示的行:
u'CI': u"C\xf4te d'Ivoire",
# -*- coding: utf-8 -*- import pycountry cc={} t = list(pycountry.countries) for country in t: cc[country.alpha2]=country.name print cc
cc 将是你要找的字典。
cc
Python源文件默认为ASCII字符编码。如果要在源代码中包含此范围之外的字符,则需要声明文件的字符编码,如 PEP 0263 。例如,将以下行添加到文件顶部可能会执行您想要的操作(假设文件以UTF-8编码):
# -*- coding: utf-8 -*-
这应该导致字符串对象包含国家/地区名称的UTF-8编码版本。如果您使用的是unicode字符串文字,那么非ASCII字符也会被正确解码。