0

我有一个关于在生成 xml 文件后格式化它们的问题。这是我的代码:

 import csv
 from xml.etree.ElementTree import Element, SubElement, Comment, tostring
 from xml.etree.ElementTree import ElementTree
 import xml.etree.ElementTree as etree

 root = Element('Solution')
 root.set('version','1.0')
 tree = ElementTree(root)

 head = SubElement(root, 'DrillHoles')
 head.set('total_holes', '238')

 description = SubElement(head,'description')


 with open ('1250_12.csv', 'r') as data:
      current_group = None
      reader = csv.reader(data)
      i = 0
      for row in reader:
          if i > 0:
               x1,y1,z1,x2,y2,z2,cost = row
               if current_group is None or i != current_group.text:
                   current_group = SubElement(description, 'hole',{'hole_id':"%s"%i})

               information = SubElement (current_group, 'hole',{'collar':', '.join((x1,y1,z1)),
                                                     'toe':', '.join((x2,y2,z2)),
                                                     'cost':    cost})
          i+=1

这会产生以下 xml 文件:

<?xml version="1.0"?>
-<Solution version="1.0">
     -<DrillHoles total_holes="238">
           -<description>
                 -<hole hole_id="1">
                        <hole toe="5797.82, 3061.01, 2576.29" cost="102.12" collar="5720.44, 3070.94, 2642.19"/></hole>

这只是 xml 文件的一部分,但足以达到此目的。我想改变很多东西,首先是我希望脚趾、成本和衣领在不同的线上,如下所示:

    <collar>0,-150,0</collar>
    <toe>69.9891,-18.731,-19.2345</toe>
    <cost>15</cost>

我希望它的顺序是衣领,然后是脚趾,然后是上面显示的成本。此外,在 xml 文件中它显示:“hole toe ="5797.82, 3061.01, 2576.29",我该如何摆脱这个洞?是的,就是这样,我对这个 python 东西真的很陌生,所以放轻松。哈哈

4

0 回答 0