无法从嵌套 xml 中获取所有数据

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])

我需要按列打印所有值,但问题是我按列打印数据,但我无法获取所有数据,因为我有一个嵌套的 xml 文件并且它具有不同数量的值。 for 循环仅达到最小数量,其余数据未显示。是否可以像 i 和 j 一样使用不同的 for 循环,并将它们附加并显示它们?

看看 itertools.izip_longest 函数,它会在空格中插入 None 并且应该可以解决您的问题

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