Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78949
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'tree': 0.05; 'lines,': 0.07; 'parser': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'badly': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:xml': 0.09; 'col': 0.16; 'csv': 0.16; 'formatted': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stdlib.': 0.16; 'wrote:': 0.18; "python's": 0.19; 'split': 0.19; 'import': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'headers': 0.24; 'looks': 0.24; 'script': 0.25; 'handling': 0.26; 'header:X-Complaints-To:1': 0.27; 'record': 0.27; 'idea': 0.28; 'xml': 0.29; "doesn't": 0.30; 'field,': 0.30; 'lines': 0.31; 'that.': 0.31; 'file:': 0.31; 'ones.': 0.31; 'file': 0.32; 'up.': 0.33; 'beginning': 0.33; 'table': 0.34; 'sense': 0.34; 'basic': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'options:': 0.36; 'url:org': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'skip:2 20': 0.60; 'skip:t 30': 0.61; 'simple': 0.61; 'name': 0.63; 'field': 0.63; 'skip:n 10': 0.64; 'invalid': 0.68; 'line,': 0.68; 'introduce': 0.78; 'fortunately': 0.84; 'route': 0.84; 'url:dynamic': 0.84; 'lucky': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Parse bad xml file |
| Date | Fri, 10 Oct 2014 14:20:04 +0200 |
| Organization | None |
| References | <9bb1f027-dc72-43c1-9301-e2105225f75a@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd859f.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14752.1412943619.18130.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1412943619 news.xs4all.nl 2839 [2001:888:2000:d::a6]:40539 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:78949 |
Show key headers only | View raw
David Jobes wrote:
> I was given a badly or poor formatted xml file that i need to convert to
> csv file:
There are no "badly formatted" XML files, only valid and invalid ones.
Fortunately following looks like the beginning of a valid one.
> <?xml version="1.0"?>
> <resultset xmlns:dyn="http://exslt.org/dynamic">
> <table name="SIGNATURE">
> <column name="ID" type="String"> </column>
> <column name="NUM" type="Integer"> </column>
> <column name="SEVERITY_ID" type="Integer"> </column>
> <column name="NAME" type="String"> </column>
> <column name="CLASS" type="String"> </column>
> <column name="PRODUCT_CATEGORY_ID" type="Integer"> </column>
> <column name="PROTOCOL" type="String"> </column>
> <column name="TAXONOMY" type="String"> </column>
> <column name="CVE_ID" type="String"> </column>
> <column name="BUGTRAQ_ID" type="String"> </column>
> <column name="DESCRIPTION" type="String"> </column>
> <column name="MESSAGE" type="String"> </column>
> <column name="FILTERTYPE" type="String"> </column>
> <data>
> <r>
> <c>00000001-0001-0001-0001-000000000027</c>
> <c>27</c>
> <c>2</c>
> <c>0027: IP Options: Record Route (RR)</c>
> <c>Network_equip</c>
> <c>10</c>
> <c>ip</c>
> <c>100741885</c>
> <c>2001-0752,1999-1339,1999-0986</c>
> <c>870</c>
> <c></c>
> <c></c>
> <c></c>
> </r>
>
>
> I have been able to load and read the file line by line,
XML doesn't have an idea of lines, so don't do that. Instead let a parser
make sense of the document structure.
> but once i get to
> the r line and try to process each c(column) that is where it blows up. I
> need to be able to split the lines and place each one or the r (row) on a
> single line for the csv.
>
> i have a list set for each one of the headers based on the col name field,
> i just have been able to format properly.
Here's a simple script using ElementTree, to introduce you to basic xml
handling with Python's stdlib. If you are lucky it might even work ;)
import csv
import sys
from xml.etree import ElementTree
SOURCEFILE = "xml_to_csv.xml"
tree = ElementTree.parse(SOURCEFILE)
table = tree.find("table")
column_names = [c.attrib["name"] for c in table.findall("column")]
writer = csv.writer(sys.stdout)
writer.writerow(column_names)
for row in table.find("data").findall("r"):
writer.writerow([field.text for field in row.findall("c")])
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parse bad xml file David Jobes <david.jobes.sony@gmail.com> - 2014-10-10 04:46 -0700
Re: Parse bad xml file Peter Otten <__peter__@web.de> - 2014-10-10 14:20 +0200
Re: Parse bad xml file David Jobes <david.jobes.sony@gmail.com> - 2014-10-10 05:33 -0700
Re: Parse bad xml file Rustom Mody <rustompmody@gmail.com> - 2014-10-12 01:37 -0700
csiph-web