xml和pandas结合处理的一个小例子-待完善

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import pandas
import json
import xml.etree.ElementTree as ET

# 读取csv对象为pandas
loc_info = pandas.read_csv('湖南花瑶花化妆品有限公司(51616兴业银行)2014.csv')
# 列值运算
loc_info["size_1"] = loc_info["right"] - loc_info["left"]
loc_info["size_2"] = loc_info["bottom"] - loc_info["top"]
# 删除列
loc_info = loc_info.drop("right",1)
loc_info = loc_info.drop("bottom",1)
loc_json = loc_info.to_json(orient='index')

# 组装xml需要的属性键值对
loc_list = []
loc_dict = json.loads(loc_json)
for loc in loc_dict.values():
    position = str(loc['left']) + ',' + str(loc['top'])
    size = str(loc['size_1']) + ',' + str(loc['size_2'])
    loc_value = {
                "Position": position,
                "Size": size,
                "StrokeThickness": "1",
                "Shape": "BasicShapes.Rectangle",
                "Content": loc['value'],
                "ItemKind": "DiagramShape"
            }
    loc_list.append(loc_value)

# 获取xml根节点
tree = ET.parse("origin.xml")
root = tree.getroot()
children = root[0][0][0]

# 添加子节点
for i in range(len(loc_list)):
    item_name = "Item"+str(i)
    ET.SubElement(children,item_name,loc_list[i])

ET.dump(children)

# 创建新xml对象
tree.write('test_demo.xml')
posted @ 2018-03-20 22:05  bingo彬哥  阅读(1923)  评论(0编辑  收藏  举报
本站总访问量