首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PYCOUNTRY将ISO 3166-1 alpha-2转换为国家/地区名称

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

Stack Overflow用户
提问于 2016-10-30 13:29:15
回答 1查看 1.5K关注 0票数 0

Python (和编程新手)在这里。我正在尝试基于目录中的文件列表生成XML文件。文件名的前两个字母对应于一个新的字母国家代码,我也在尝试将其提取出来。

我的预期格式如下:

代码语言:javascript
复制
<ROOT>
    <BASIC/>
    <FULL>
        <INFO>
            <server>filname</server>
            <country>country</country>
            <region/>
        </INFO>
    </FULL>
</ROOT>

我似乎能够生成XML文件,但是我无法使用pycountry将两位数的国家代码转换为国家。有没有人能提出一个可行的解决方案?对其余代码的任何注释也会很有帮助。

代码语言:javascript
复制
# -*- 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")
EN

回答 1

Stack Overflow用户

发布于 2016-11-04 12:08:37

感谢gbe的帮助!它并不美观,但以下是运行正常的代码。

代码语言:javascript
复制
# -*- 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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40326284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档