Note that there are some explanatory texts on larger screens.

plurals
  1. POXML to CSV formatting
    primarykey
    data
    text
    <p>I have a program that converts csv to xml to csv. However, when i am converting it back to csv, the formatting has gone wrong. Originally the csv file is like so:</p> <pre><code>x1 y1 z1 x2 y2 z2 cost 1 2 3 4 5 6 7 </code></pre> <p>and so on and so forth. This data is also represented using excel. I then convert this to xml like so:</p> <pre><code>&lt;Solution version="1.0"&gt; &lt;DrillHoles total_holes="238"&gt; &lt;description&gt; &lt;hole hole_id="1"&gt; &lt;collar&gt;1, 2, 3&lt;/collar&gt; &lt;toe&gt;4, 5, 6&lt;/toe&gt; &lt;cost&gt;7&lt;/cost&gt; &lt;/hole&gt; </code></pre> <p>*note that is only one part of the whole thing but it is enough for this example. SO when i convert this back into csv format it appears to be like this:</p> <pre><code> x1 y1 z1 x2 y2 z2 cost 123 456 7 </code></pre> <p>where x1y1z1x2y2z2cost are jumbled up in one column in excel . Also this is represented in a excel.</p> <p>Here is my code for generating xml:</p> <pre><code>def generate_xml(reader,outfile): root = Element('Solution') root.set('version','1.0') tree = ElementTree(root) head = SubElement(root, 'DrillHoles') description = SubElement(head,'description') current_group = None i = 1 for row in reader.next(): 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}) collar = SubElement(current_group,'collar') toe = SubElement(current_group,'toe') cost1 = SubElement(current_group,'cost') collar.text = ', '.join((x1,y1,z1)) toe.text = ', '.join((x2,y2,z2)) cost1.text = cost i+=1 head.set('total_holes', '%s'%i) indent.indent(root) tree.write(outfile) </code></pre> <p>generating csv: def generate_csv(root, outfile): with open(outfile, 'w') as file_:</p> <pre><code> writer = csv.writer(file_, delimiter="\t") writer.writerow(['x1'] + ['y1'] + ['z1'] + ['x2'] + ['y2'] + ['z2'] + ['cost']) for a in zip(root.findall("DrillHoles/description/hole/collar"), root.findall("DrillHoles/description/hole/toe"), root.findall("DrillHoles/description/hole/cost")): writer.writerow([x.text for x in a]) </code></pre> <p>please help thanks edit: i think i might need multiple delimiters but i do not know how to incorporate that into this program.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload