简体   繁体   English

使用PYCOUNTRY将ISO 3166-1 alpha-2转换为国家名称

[英]Use PYCOUNTRY to convert ISO 3166-1 alpha-2 to Country Name

Python (and coding newbie) here. Python(和编码新手)在这里。 I'm attempting to generate an XML file based a list of files in a directory. 我正在尝试根据目录中文件列表生成XML文件。 The first two letters of the filenames correspond to a new letter country code and I'm trying to extract this as well. 文件名的前两个字母对应于新的字母国家/地区代码,我也尝试将其提取出来。

My intended format is as follows: 我的预期格式如下:

<ROOT>
    <BASIC/>
    <FULL>
        <INFO>
            <server>filname</server>
            <country>country</country>
            <region/>
        </INFO>
    </FULL>
</ROOT>

I seem to be able to generate the XML file but I'm unable to convert the two digit country code to the country using pycountry. 我似乎能够生成XML文件,但是无法使用pycountry将两位数的国家/地区代码转换为国家/地区。 Could someone please suggest a possible solution? 有人可以建议一个可能的解决方案吗? Any comments on the rest of the code would be helpful as well. 对其余代码的任何注释也将有所帮助。

# -*- coding: utf-8 -*-
import lxml.etree as xml
import pycountry
import glob

import gettext
gettext.bindtextdomain('iso3166', pycountry.LOCALES_DIR)
_c = lambda t: gettext.dgettext('iso3166', t)

def createXML(outfile):
        root = xml.Element("ROOT")
        basic = xml.Element("BASIC")
        full = xml.Element("FULL")
        root.append(basic)
        root.append(full)
# add file information
        for filename in glob.glob("*.*"):
                info = xml.Element("INFO")
                server = xml.SubElement(info, "server")
                server.text = filename
                short = filename[:2]
                country = xml.SubElement(info, "country")
                def get_country(code):
                  return _c(pycountry.countries.get(alpha2=code).name)
                country.text = get_country(short)
                region = xml.SubElement(info, "region")
                full.append(info)
        print xml.tostring(root, pretty_print=True)
#save new XML
#       tree = xml.ElementTree(root)
#       with open(filename, "w") as fh:
#        tree.write(fh)

#--------------------------------------------------------
if __name__ == "__main__":
    createXML("info.xml")

Thanks to gbe for the help! 感谢gbe的帮助! It's not pretty but here is the code that worked. 它不是很漂亮,但是这里的代码起作用了。

# -*- coding: utf-8 -*-
import lxml.etree as xml
import pycountry
import glob

import gettext
gettext.bindtextdomain('iso3166', pycountry.LOCALES_DIR)
_c = lambda t: gettext.dgettext('iso3166', t)

def createXML(outfile):
        root = xml.Element("ROOT")
        basic = xml.Element("BASIC")
        full = xml.Element("FULL")
        root.append(basic)
        root.append(full)
# add file information
        for filename in glob.glob("*.*"):
                info = xml.Element("INFO")
                server = xml.SubElement(info, "server")
                server.text = filename
                short = filename[:2].upper()
                country = xml.SubElement(info, "country")
                country.text = pycountry.countries.get(alpha2=short).name
                region = xml.SubElement(info, "region")
                full.append(info)
        print xml.tostring(root, pretty_print=True)
#save new XML
#       tree = xml.ElementTree(root)
#       with open(filename, "w") as fh:
#        tree.write(fh)

#--------------------------------------------------------
if __name__ == "__main__":
    createXML("info.xml")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以让 Plotly choropleth 使用 ISO 3166-1 alpha-2 国家代码而不是 ISO 3166-1 alpha-3 来映射国家? - Is it possible to make Plotly choropleth use ISO 3166-1 alpha-2 country codes instead of ISO 3166-1 alpha-3 to map countries? 如何使用 python 将国家名称转换为 ISO 3166-1 alpha-2 值 - How to convert country names to ISO 3166-1 alpha-2 values, using python 如何根据 ISO-3166-1 alpha-2 代码获取国家/地区名称? - How do I get the country name based on the ISO-3166-1 alpha-2 code? 使用 pycountry 和 gettext 将翻译的国家/地区名称转换为 alpha2 - convert translated country-names to alpha2 with pycountry and gettext 使用 pycountry 从国家获取大陆名称 - Get continent name from country using pycountry 使用 pycountry 从名称中获取国家/地区 ID - Get the country id from the name with pycountry “DE”alpha2 国家/地区代码的 PyCountry 货币格式问题 - PyCountry currency formatting woes for 'DE' alpha2 country code 如何使用 pycountry_convert 在 DataFrame 中创建一个新列,列出该国家/地区所在的大陆? - How to use pycountry_convert to create a new column in a DataFrame that lists the continent that the country is in? 循环pycountry转换 - loop the pycountry convert 使用 pycountry 从 iso-639 代码中获取完整的语言名称 - Get full language name from iso-639 code using pycountry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM