Python版本2.7:XML ElementTree:如何遍历子元素的某些元素以找到匹配项 [英] Python version 2.7: XML ElementTree: How to iterate through certain elements of a child element in order to find a match

查看:82
本文介绍了Python版本2.7:XML ElementTree:如何遍历子元素的某些元素以找到匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名编程新手,很少使用python,所以请在我尝试解释我要做什么时忍受:)

I'm a programming novice and only rarely use python so please bear with me as I try to explain what I am trying to do :)

我有以下XML:

<?xml version = "1.0" encoding = "utf-8"?>
<Patients>
    <Patient>
               <PatientCharacteristics>
                   <patientCode>3</patientCode>
               </PatientCharacteristics>
               <Visits>
                   <Visit>
                          <DAS>
                               <CRP>14</CRP>
                               <ESR/>
                               <Joints>
                                       <DAS_PROFILE>28/28</DAS_PROFILE>
                                       <SWOL28>20</SWOL28>
                                       <TEN28>20</TEN28>
                               </Joints>
                          </DAS>
                          <VisitDate>2010-02-17</VisitDate>
                   </Visit>
                   <Visit>
                          <DAS>
                               <CRP>10</CRP>
                               <ESR/>
                               <Joints>
                                       <DAS_PROFILE>28/28</DAS_PROFILE>
                                       <SWOL28>15</SWOL28>
                                       <TEN28>20</TEN28>
                               </Joints>
                          </DAS>
                          <VisitDate>2010-02-10</VisitDate>
                   </Visit>
               </Visits>
    </Patient>
    <Patient>
        <PatientCharacteristics>
                   <patientCode>3</patientCode>
        </PatientCharacteristics>
               <Visits>
                   <Visit>
                          <DAS>
                               <CRP>14</CRP>
                               <ESR/>
                               <Joints>
                                       <DAS_PROFILE>28/28</DAS_PROFILE>
                                       <SWOL28>34</SWOL28>
                                       <TEN28>0</TEN28>
                               </Joints>
                          </DAS>
                          <VisitDate>2010-08-17</VisitDate>
                   </Visit>
                   <Visit>
                          <DAS>
                               <CRP>10</CRP>
                               <ESR/>
                               <Joints>
                                       <DAS_PROFILE>28/28</DAS_PROFILE>
                                       <SWOL28></SWOL28>
                                       <TEN28>2</TEN28>
                               </Joints>
                          </DAS>
                          <VisitDate>2010-07-10</VisitDate>
                   </Visit>
                   <Visit>
                          <DAS>
                               <CRP>9</CRP>
                               <ESR/>
                               <Joints>
                                       <DAS_PROFILE>28/28</DAS_PROFILE>
                                       <SWOL28>56</SWOL28>
                                       <TEN28>6</TEN28>
                               </Joints>
                          </DAS>
                          <VisitDate>2009-07-10</VisitDate>
                   </Visit>
               </Visits>

    </Patient>
</Patients>

我要做的就是更新某些'SWOL28'值(如果它们与PatientCode和VisitDate匹配)我已经存储在一个文本文件中。据我了解,elementtree不包含父引用,就好像它包含父引用一样,我可以只从根目录使用findall()并从那里反向进行工作。现在的样子是我的伪代码:

All I want to do here is update certain 'SWOL28' values if they match the patientCode and VisitDate that I have stored in a text file. As I understand, elementtree does not include a parent reference, as if it did, I could just use findall() from the root and work backwards from there. As it stands here is my psuedocode:


  1. 对于文本文件中的每一行:

  2. 输入将Visit_Date Patient_Code New_SWOL28放入变量中

  3. 对于每个患者元素:

  4. 如果PatientCode = Patient_Code

  5. 每次访问元素:

  6. 如果VisitDate = Visit_Date

  7. 如果此次访问存在SWOL28元素

  8. 将SWOL28更新为New_SWOL28

  1. For each line in the text file:
  2. Put Visit_Date Patient_Code New_SWOL28 into variables
  3. For each patient element:
  4. If patientCode = Patient_Code
  5. For each Visit element:
  6. If VisitDate = Visit_Date
  7. If SWOL28 element exists for this visit
  8. Update SWOL28 to New_SWOL28

但是我停留在第5步。如何获得要迭代访问的列表?道歉,如果这是一个非常愚蠢的问题,但我向您高低地寻找答案,我向您保证!我已经将代码简化为需要在下面修复的部分的裸露示例:

But I am stuck at step number 5. How do I get a list of visits to iterated through? Apologies if this is a very dumb question but I have searched high and low for an answer I assure you! I have stripped down my code to the bare example of the part I need to fix below:

import xml.etree.ElementTree as ET
tree = ET.parse('DB3.xml')
root = tree.getroot()
for child in root: # THIS GETS ME ALL THE PATIENT ATTRIBUTES
    print child.tag 
    for x in child/Visit: # THIS IS WHAT I CANNOT FIND THE CORRECT SYNTAX FOR
        # I WOULD THEN PERFORM STEPS 6, 7 AND 8 HERE

我将非常感谢你们中任何人可能对此有任何想法。我当然不是编程自然人!

I would be deeply appreciative of any ideas any of you may have on this. I am not a programming natural that's for sure!

在此先感谢
Sarah

Thanks in advance, Sarah

编辑1:

根据以下SVK的建议,我尝试了以下操作:

On the advice of SVK below I tried the following:

import xml.etree.ElementTree as ET
tree = ET.parse('Untitled.xml')
root = tree.getroot()
for child in root:
    print child.tag 
    child.find( "visits" )
    for x in child.iter("visit"):
        print x.tag, x.text

但我得到的唯一输出是:
患者
患者
,没有一个较低的标签。有想法吗?

But the only output I get is: Patient Patient and none of the lower tags. Any ideas?

推荐答案

未经测试,它应该与您想要的相当接近。

This is untested by it should be fairly close to what you want.

for patient in root:
    patient_code =  patient.find('PatientCharacteristics').find('patientCode')
    if patient_code.text == code:
            for visit in patient.find('Visits'):
                    visit_date = visit.find('VisitDate')
                    if visit_date.text == date:
                        swol28 = visit.find('DAS').find('Joints').find('SWOL28')
                        if swol28.text:
                            visit.find('DAS').find('Joints').set('SWOL28', new_swol28)

这篇关于Python版本2.7:XML ElementTree:如何遍历子元素的某些元素以找到匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆