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

Python 无法从嵌套xml中获取所有数据,python,xml,xml-parsing,elementtree,xml.etree,Python,Xml,Xml Parsing,Elementtree,Xml.etree,我需要按列打印所有值,但问题是我按列打印数据,但我无法获取所有数据,因为我有一个嵌套的xml文件,它有不同数量的值。for循环仅达到最小值,其余数据未显示。是否可以使用不同的for循环,如i和j,并将它们都追加并显示?查看该函数,它将在空白处不插入任何内容,应该可以解决您的问题 行=列表(itertools.izip_longest(代码列表、代码系统列表、代码系统名称、显示名称、代码、代码、代码、状态、时间)) 对于行中的行: 打印(行) 您能给我们看一下xml文件吗?否则怎么能在空白处打印

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

查看该函数,它将在空白处不插入任何内容,应该可以解决您的问题


行=列表(itertools.izip_longest(代码列表、代码系统列表、代码系统名称、显示名称、代码、代码、代码、状态、时间))
对于行中的行:
打印(行)

您能给我们看一下xml文件吗?否则怎么能在空白处打印一个都不?它非常大。例如,在productCode中,我有10个代码和8个代码系统,如何使用上述逻辑按列打印它们。请帮助我。请提供。我得到的前四列为无。代码,代码。。。它们再次嵌套在代码列表、代码系统列表……代码、代码。。。是标签下的子标签,在上面的xml文件中。我不确定您到底想实现什么,代码将从每个列表中获取元素,并形成一个新列表。如果列表中的元素数量较少,则在创建列表时将填充“无”,这就是为什么您看到无的原因,如果前四列完全没有,这意味着这些列表没有任何值,那么可以打印整个列表并查看它们是否有数据。首先,请确保使用findAll已根据需要解析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])