python 读写XML

Posted 百变小超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 读写XML相关的知识,希望对你有一定的参考价值。

参考

http://yshblog.com/blog/151

# coding:utf-8
import os
import sys
from lxml import etree

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from abbr_extractor.pgconnect import PgConnect
from common_tool.common_utils import get_latest_database_name, region_version
from db_options import get_all_host_options, get_all_database, select_u_turn_info


def collect_all_u_turn_info():
    host_option_list = get_all_host_options()
    # get all database name
    database_host_dic = {}
    database_name_list = []
    for host_option in host_option_list:
        pg_conn = PgConnect(host_option)
        if pg_conn.init_db():
            host_database_list = get_all_database(pg_conn)
            database_name_list += host_database_list
            for name in host_database_list:
                database_host_dic[name] = host_option
    # get the latest database
    latest_name_list = get_latest_database_name(database_name_list)
    print latest_name_list

    # sql uturn
    u_turn_info_list_all = []
    for database_name in latest_name_list:
        region, version = region_version(database_name)
        host_option = database_host_dic.get(database_name)
        host_option.dbname = database_name
        pg_conn = PgConnect(host_option)
        if pg_conn.init_db():
            u_turn_info_list = select_u_turn_info(pg_conn)
            for u_turn_info in u_turn_info_list:
                setattr(u_turn_info, "region", region)
            u_turn_info_list_all += u_turn_info_list

    return u_turn_info_list_all


def save_u_turn_to_xml(u_turn_info_list, save_xml_filename):
    root = etree.Element(‘u-turn_config‘, nsmap={"xmlns": ‘http://www.w3.org/2001/XMLSchema-instance‘})

    for u_turn in u_turn_info_list:
        record_node = etree.Element(‘record‘)
        root.append(record_node)

        admin_place_id_node = etree.Element(‘admin_place_id‘)
        record_node.append(admin_place_id_node)
        admin_place_id_node.text = "%s" % u_turn.admin_place_id

        admin_type_node = etree.Element(‘admin_type‘)
        record_node.append(admin_type_node)
        admin_type_node.text = "%s" % u_turn.admin_type

        admin_name_node = etree.Element("admin_name")
        record_node.append(admin_name_node)
        if isinstance(u_turn.admin_name, str):
            u_turn.admin_name = unicode(u_turn.admin_name, ‘utf-8‘)
        admin_name_node.text = u_turn.admin_name

        country_node = etree.Element("country")
        record_node.append(country_node)
        country_node.text = u_turn.country

        region_node = etree.Element("region")
        record_node.append(region_node)
        region_node.text = u_turn.region.lower()

    tree = etree.ElementTree(root)
    tree.write(save_xml_filename, pretty_print=True, xml_declaration=True, encoding=‘utf-8‘)
    print "write content to [%s] finish." % save_xml_filename


def read_u_turn_xml(xml_file_path):
    xml = etree.parse(xml_file_path)
    root = xml.getroot()
    u_turn_info_list = []
    for recode_node in root.getchildren():
        from db_options import Options
        u_turn_info = Options()
        for tag_node in recode_node.getchildren():
            tag_name = tag_node.tag
            tag_text = tag_node.text
            setattr(u_turn_info, tag_name, tag_text)
        u_turn_info_list.append(u_turn_info)

    return u_turn_info_list


if __name__ == ‘__main__‘:
    u_turn_list = collect_all_u_turn_info()
    save_u_turn_to_xml(u_turn_list, ‘uturn_auto.xml‘)

    # u_turn_list = read_u_turn_xml(‘uturn.xml‘)
    # u_turn_list.sort(key=lambda d: d.admin_place_id)
    # save_u_turn_to_xml(u_turn_list, ‘uturn_sort.xml‘)

  遇到的问题有:

写入时候的编码问题,将str类型转换为 unicode类型即可。

以上是关于python 读写XML的主要内容,如果未能解决你的问题,请参考以下文章

python 读写XML

聊一聊使用 Python 读写 XML

python+selenium自动化软件测试(第12章):Python读写XML文档

PythonPython XML 读写

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException