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

2024-09-27 17:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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循环只达到最小值,其余数据不显示。有没有可能使用一个不同的for循环,比如i和j,并附加它们并显示它们?你知道吗


Tags: textinfordoccodexmltranslationappend
1条回答
网友
1楼 · 发布于 2024-09-27 17:33:33

看看itertools.izip_longest函数,它不会在空格中插入任何内容,应该可以解决您的问题

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

相关问题 更多 >

    热门问题