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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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. > > > > > > > > > > > > > > > > > > > 00000001-0001-0001-0001-000000000027 > 27 > 2 > 0027: IP Options: Record Route (RR) > Network_equip > 10 > ip > 100741885 > 2001-0752,1999-1339,1999-0986 > 870 > > > > > > > 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")])