【问题标题】:xml file from excel sheet dataexcel工作表数据中的xml文件
【发布时间】:2015-10-30 17:58:38
【问题描述】:

你好使用下面的代码sn-p,我在下面创建了一个xml,但我注意到:我在代码中使用的参数顺序与输出中的不同,即<node y="-3749099.0" x="-45194.0" id="11542.0"/>应该是<node id="11542.0" x="-45194.0" y="-3749099.0"/>也是输出不是下面所需的输出。有人可以建议我怎么做:

  • 更正我的代码以获得正确的输出
  • 扩展代码,这样如果我必须使用包含许多列(超过 3 列)的 excel 文件,我就不必像 FIELD(id=str(val[0]), x=str(val[1]), y=str(val[2])), 那样硬编码 val[0], val[1], val[2]

代码片段:

import lxml.etree
import lxml.builder
import xlrd

wb = xlrd.open_workbook("emme_nodes1.xls")
sh = wb.sheet_by_index(0)
tags = [n.replace(" ", "").lower() for n in sh.row_values(0)]

for row in range(1, sh.nrows):
    val = sh.row_values(row)

    E = lxml.builder.ElementMaker()
    ROOT = E.network
    DOC = E.nodes
    FIELD = E.node
    my_doc = ROOT(
            DOC(
                FIELD(id=str(val[0]), x=str(val[1]), y=str(val[2])),
                )
            )
    print lxml.etree.tostring(my_doc, pretty_print=True)

输出:

<network>
  <nodes>
    <node y="-3748681.0" x="-45333.0" id="11543.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3747847.0" x="-44369.0" id="11540.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3748683.0" x="-45060.0" id="11541.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3750248.0" x="-45518.0" id="11546.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3750024.0" x="-45448.0" id="11547.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3749821.0" x="-44745.0" id="11544.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3750508.0" x="-45561.0" id="11545.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3750202.0" x="-45802.0" id="11548.0"/>
  </nodes>
</network>

<network>
  <nodes>
    <node y="-3749805.0" x="-45485.0" id="11549.0"/>
  </nodes>
</network>

期望的输出:

<network>
  <nodes>
    <node id="11542.0" x="-45194.0" y="-3749099.0"/>
    <node id="11543.0" x="-45333.0" y="-3748681.0"/>
    <node id="11540.0" x="-44369.0" y="-3747847.0"/>
    <node id="11541.0" x="-45060.0" y="-3748683.0"/>
    <node id="11546.0" x="-45518.0" y="-3750248.0"/>
    <node id="11547.0" x="-45448.0" y="-3750024.0"/>
    <node id="11544.0" x="-44745.0" y="-3749821.0"/>
    <node id="11545.0" x="-45561.0" y="-3750508.0"/>
    <node id="11549.0" x="-45485.0" y="-3749805.0"/>
    <node id="11548.0" x="-45802.0" y="-3750202.0"/>
    <node id="11549.0" x="-45485.0" y="-3749805.0"/>
  </nodes>
</network>

Excel工作表(emme_nodes1.xls):

 id         x           y
11542   -45194.0    -3749099.0
11543   -45333.0    -3748681.0
11540   -44369.0    -3747847.0
11541   -45060.0    -3748683.0
11546   -45518.0    -3750248.0
11547   -45448.0    -3750024.0
11544   -44745.0    -3749821.0
11545   -45561.0    -3750508.0
11548   -45802.0    -3750202.0
11549   -45485.0    -3749805.0

【问题讨论】:

  • XML 中的属性顺序完全不相关。如果您构建一个依赖于属性顺序的系统,那么您就犯了一个错误。不要那样做。
  • @Tomalak,即使 xml 文件将用作不同应用程序的输入,这也适用吗?
  • @Nobi 是的。如果其他应用程序正确读取 XML,则属性顺序无关紧要。 (有很多写得很糟糕的应用程序实际上并没有处理 XML,只是有人对其中的一些理解不完整)
  • @dsh 这总是适用。属性顺序在规范中被声明为不相关的权利。 (我也很难想象一个理智的应用程序需要属性按特定顺序排列,因为这会建议应用程序对 XML 使用字符串函数,而您绝不能对 XML 使用字符串函数。)
  • 我也有类似需求,看看能不能帮到你github.com/newPrimitives/xls2xml

标签: python xml xls


【解决方案1】:

跟踪代码的执行。

你拥有的是这样的:

  1. 第一行:

    1. 创建一个新的 XML 文档构建器
    2. 创建一个新的根元素
    3. 创建一个新的“网络”元素
    4. 添加到根节点
    5. 打印整个文档
  2. 第二行:

    1. 创建一个新的 XML 文档构建器
    2. 创建一个新的根元素
    3. 创建一个新的“网络”元素
    4. 添加到根节点
    5. 打印整个文档
  3. 第三行: 重复


你想做的是这样的:

  1. 创建一个新的 XML 文档构建器
  2. 创建一个新的根元素
  3. 第一行:
    1. 创建一个新的“网络”元素
    2. 添加到根节点
  4. 第二行:
    1. 创建一个新的“网络”元素
    2. 添加到根节点
  5. ...
  6. 打印整个文档

看看你是否可以相应地更改你的代码并更新它。

【讨论】:

    【解决方案2】:

    我终于找到了这个解决方案,而且效果很好

    import xlrd
    from lxml import etree
    
    root = etree.Element('network')
    root.set('name', 'Network')
    tree = etree.ElementTree(root)
    name = etree.Element('nodes')
    root.append(name)   
    wb = xlrd.open_workbook("emme_nodes1.xls")
    sh = wb.sheet_by_index(0)
    
    for row in range(1, sh.nrows):
        val = sh.row_values(row)
        element = etree.SubElement(name, 'node')
        element.set('id', str(int(val[0])))
        element.set('x', str(val[1]))
        element.set('y', str(val[2]))
    print etree.tostring(root,pretty_print=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2015-02-15
      • 2012-08-28
      相关资源
      最近更新 更多