PythonXml文件添加字节属性的方法-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

PythonXml文件添加字节属性的方法-创新互联

实例如下所示:

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的崇川网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
from xml.etree.cElementTree import ElementTree,Element
import xlrd
import re
def read_xlsx(xlsx_path):
  workbook = xlrd.open_workbook(xlsx_path)
  booksheet = workbook.sheet_by_name("Sheet1")
  p = list()
  row_data = []
  for row in range(booksheet.nrows):
    for col in range(booksheet.ncols):
      cel = booksheet.cell(row, col)
      val = cel.value
      try:
        val = cel.value
        val = re.sub(r'\s+', '', val)
      except:
        pass
      if type(val) == float:
        val = int(val)
      else:
        val = str(val)
      row_data.append(val)
  p.append(row_data)
  return p
def read_xml(Int_path):
  tree = ElementTree()
  tree.parse(Int_path)
  return tree
def write_xml(tree, out_path):
  tree.write(out_path,encoding="utf-8",xml_declaration=True)
  '''tree.write('output.xml')'''
def if_match(node,k_name,k_val):
  c = node.get(k_name)
  if node.get(k_name) == k_val:
    return True
  return False
def find_nodes(tree,path):
  return tree.findall(path)
def get_node_by_keyvalue(nodelist,key_name,key_val):
  result_nodes=[]
  for node in nodelist:
    if if_match(node,key_name,key_val):
      result_nodes.append(node)
  return result_nodes
def add_node_properties(nodelist,add_key,add_properties):
  for node in nodelist:
    node.set(add_key,add_properties)
  return node
if __name__ == "__main__":
  tree = read_xml("./ContentList.xml")
  nodes = find_nodes(tree,"Content")
  P = read_xlsx("./ServiceId.xlsx")
  a = P[0]
  i = 0
  while i < 80:
    result_nodes = get_node_by_keyvalue(nodes , "AssetId" , a[i])
    add_node_properties(result_nodes,"ServiceId",a[i+1])
    i = i + 2
  write_xml(tree,'./ContentList1.xml')

文章名称:PythonXml文件添加字节属性的方法-创新互联
文章起源:http://kswsj.cn/article/cspigs.html

其他资讯