Python Forum
Why this code not getting desired output ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this code not getting desired output ?
#1
Thanks for your response to my threads.

I am trying to use the pyhton code below. I am getting weird output.

Python Code

def Process_XML(infile, inxpath, xpathdln):
    """
       Process XML Xpath
    """   
    import xml.etree.ElementTree as ET
    from lxml import etree

    tree = etree.parse(infile)
    root = tree.getroot()
    print(root)

    for dln in tree.xpath(xpathdln):    
        # Iterate over attributes of datafield
            print(dln.tag + ' = ' + dln.text)
            for df in tree.xpath(inxpath):    
        # Iterate over attributes of datafield
                    print(df.tag + ' = ' + df.text)
                    for attrib_name in df.attrib:
                                    print( '@' + attrib_name + '=' + df.attrib[attrib_name])

        # subfield is a child of datafield, and iterate
                                    subfields = df.getchildren()
                                    for subfield in subfields:
                                                 print (subfield.tag + ' = ' + subfield.text)
    return;                
                
infile1 = 'D:\Python_work\eclipse-workspace\My1stPythonP\input.xml'
inxpath1 = '/DataFileFor/DataR/Ret/W2'
xpathdln = '/DataFileFor/DataR/NO'
Process_XML(infile1, inxpath1, xpathdln) 
I have a large XML file like (inputf.xml). I used this file as input = inputf.xml in posted code

Output:
<?xml version="1.0" encoding="UTF-8"?> <DataFileFor> <DataR> <Id>5070022019330a0050hq</Id> <NUM>30221730001019</NUM> <Postmark>2020-01-03T09:25:57.000-05:00</Postmark> <TNO>47647</TNO> . . . . . </DataFileFor>
When grab the XPATH of Node using xml_grep, I am getting.

xml_grep DataFileFor/DataR/Ret/W2 inputf.xml ===> output

Output:
<?xml version="1.0" ?> <xml_grep version="0.7" date="Fri Jun 26 13:07:11 2020"> <file filename="inputf.xml"> <W2 Id="W2" dName="W2" sId="00000000" sVersionNum="String"> <CorrectedW2Ind>X</CorrectedW2Ind> <EmployeeSSN>000000000</EmployeeSSN> <EmployerEIN>000000000</EmployerEIN> <EmployerNameControlTxt>S</EmployerNameControlTxt> <EmployerName> <BusinessNameLine1Txt>String</BusinessNameLine1Txt> <BusinessNameLine2Txt>String</BusinessNameLine2Txt> </EmployerName> <EmployerUSAddress> <AddressLine1Txt>String</AddressLine1Txt> <AddressLine2Txt>String</AddressLine2Txt> <CityNm>String</CityNm> <StateAbbreviationCd>AL</StateAbbreviationCd> <ZIPCd>000000000</ZIPCd> . . . . . </W2>
When run the code , I am getting

<Element DataFileFor at 0x2779a00>
NO = 30221730001019

It is working for small file. My expectation is same as xml_grep output.

I am strugling to find why xml_grep able to extract the xpath for this file, but posted code is not able to do it.

The attributes

Id="W2" dName="W2" sId="00000000" sVersionNum="String"> are not showing up in the output

What are the changes required to the code, to fix this?

Thanks for your guidance.
Reply
#2
Any idea for this issue ?
Reply
#3
Any thoughts ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 302 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  I cannot able to see output of this code ted 1 715 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  why I dont get any output from this code William369 2 1,084 Jun-23-2022, 09:18 PM
Last Post: William369
  How can I organize my code according to output that I want ilknurg 1 1,138 Mar-11-2022, 09:24 AM
Last Post: perfringo
  how can I display only desired items? 3lnyn0 5 1,974 Dec-25-2021, 06:49 PM
Last Post: menator01
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,601 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Not rounding to desired decimal places? pprod 2 2,505 Mar-05-2021, 11:11 AM
Last Post: pprod
  showing only desired part of a plot grknkilicaslan 1 2,291 Jul-10-2020, 03:51 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020