简体   繁体   中英

xml read using panda in python

i have a xml file.i trying to read it in a usual way as shown below

def xmlfilereadread(self,path):
    doc = minidom.parse(path)
    Account = doc.getElementsByTagName("sf:ReceiverSet")[0]
    num = Account.getAttribute('totalNo')
    aList = []
    for i in range(int(num)):
        print(i)
        AccountReference = doc.getElementsByTagName("sf:Receiver")[i] 

but i need to use panda unstead of this code.how can i read data.my sample xml code is

<?xml version="1.0" encoding="UTF-8"?>
<sf:IFile xmlns:sf="http://www.canadapost.ca/smartflow" sequenceNo="10">   
<sf:ReceiverSet documentTypes="TAXBILL" organization="lincolntax" totalNo="3">  
<sf:Receiver sequenceNo="1" correlationID="1114567890123456789">   
<sf:AccountReference>11145678901234567891111</sf:AccountReference>   
<sf:SubscriptionAuth> <sf:ParamSet>   
<sf:Param name="auth1">1114567890123456789</sf:Param>   
<sf:Param name="auth2">CARTER, JOE</sf:Param> </sf:ParamSet>   
</sf:SubscriptionAuth>  
</sf:Receiver> <sf:Receiver sequenceNo="2" correlationID="2224567890123456789">   
<sf:AccountReference>22245678901234567892222</sf:AccountReference> <sf:SubscriptionAuth> <sf:ParamSet>  
<sf:Param name="auth1">2224567890123456789</sf:Param>   
<sf:Param name="auth2">DOE, JANE</sf:Param> </sf:ParamSet>   
</sf:SubscriptionAuth> </sf:Receiver> <sf:Receiver sequenceNo="3" correlationID="3334567890123456789">   
<sf:AccountReference>33345678901234567893333</sf:AccountReference> <sf:SubscriptionAuth> <sf:ParamSet>  
<sf:Param name="auth1">3334567890123456789</sf:Param> <sf:Param name="auth2">SOZE, KEYSER</sf:Param>  
</sf:ParamSet> </sf:SubscriptionAuth> </sf:Receiver> </sf:ReceiverSet> </sf:IFile>

XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level

.

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

Or you can use lxml

from lxml import etree

root = etree.parse(r'local-path-to-.xml')
print (etree.tostring(root))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM