码迷,mamicode.com
首页 > 编程语言 > 详细

python 读写XML

时间:2017-11-08 22:25:14      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:ext   ==   sys.path   encoding   extra   etc   xmlns   node.app   content   

参考

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

标签:ext   ==   sys.path   encoding   extra   etc   xmlns   node.app   content   

原文地址:http://www.cnblogs.com/dasheng-maritime/p/7806355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!