简体   繁体   中英

Unable to fetch all data from nested xml

import xml.etree.ElementTree as ET

tree = ET.parse("D:\Parsed_CCD.xml")
doc = tree.getroot()


codeList=[]
codeSystemList=[]
codeSystemName=[]
displayName=[]
code=[]
codeS=[]
codeN=[]
display=[]
status=[]
stime=[]
etime=[]


for elem1 in doc.findall('.//medicationsInfo/entryInfo/productCode/code'):
    codeList.append(elem1.text)

for elem2 in doc.findall('.//medicationsInfo/entryInfo/productCode/codeSystem'):
    codeSystemList.append(elem2.text)


for elem3 in doc.findall('.//medicationsInfo/entryInfo/productCode/codeSystemName'):
    codeSystemName.append(elem3.text)

for elem4 in doc.findall('.//medicationsInfo/entryInfo/productCode/displayName'):
    displayName.append(elem4.text)  

for elem5 in doc.findall('.//medicationsInfo/entryInfo/productCode/translation/code'):
    code.append(elem5.text) 

for elem6 in doc.findall('.//medicationsInfo/entryInfo/productCode/translation/codeSystem'):
    codeS.append(elem6.text)    

for elem7 in doc.findall('.//medicationsInfo/entryInfo/productCode/translation/codeSystemName'):
    codeN.append(elem7.text)

for elem9 in doc.findall('.//medicationsInfo/entryInfo/productCode/translation/displayName'):
    display.append(elem9.text)  

for elem8 in doc.findall('.//medicationsInfo/entryInfo/statusCode'):
    status.append(elem8.text)

for elem10 in doc.findall('.//medicationsInfo/entryInfo/startTime'):
    stime.append(elem10.text)

for elem11 in doc.findall('.//medicationsInfo/entryInfo/endTime'):
    etime.append(elem11.text)


for i in range(len(codeList)):
    print (codeList[i],codeSystemList[i],codeSystemName[i],displayName[i],code[i],codeS[i],codeN[i],status[i],etime[i])

I need to print all values column-wise, but the problem is I am printing data column-wise, but I am not able to fetch all the data because I have a nested xml file and it has different number of values. The for loop is reaching only minimum number and the rest of the data is not displaying. Is it possible to use a different for loop like i and j, and append them both and display them?

看看itertools.izip_longest函数,它将在空白处插入None并应解决您的问题

rows=list(itertools.izip_longest(codeList,codeSystemList,codeSystemName,displayName,code,codeS,codeN,status,etime)) for row in rows: print(row)

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