使用大型XML文件,Python和BS4,我想在整个过程中添加子子元素。
我想在每个单独的<Learner>
标签内添加3个元素,分别称为DB-RU,LAD-RU和LAW-RU。
这些字段可以用字符串“ Null”填充
使用下面的模拟XML数据
我尝试使用元素树,但发现它增加了多个问题。 1是将</ns0:>
添加到所有元素名称。所以想尽可能使用BS4
下面是2位学习者的XML Mock数据(不过,可能要为数百名学习者运行。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Please note that this file is properly formed, and serves as an example of a file that will load into the ILR DC system. The data is anonymised and does not refer to a real-world provider, learning delivery or learner. Based on the ILR specification, version 2, dated April 2018-->
<Message xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="ESFA/ILR/2018-19" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ESFA/ILR/2018-19">
<Header>
<CollectionDetails>
<Collection>ILR</Collection>
<Year>1819</Year>
<FilePreparationDate>2018-01-07</FilePreparationDate>
</CollectionDetails>
<Source>
<ProtectiveMarking>OFFICIAL-SENSITIVE-Personal</ProtectiveMarking>
<UKPRN>99999999</UKPRN>
<SoftwareSupplier>SupplierName</SoftwareSupplier>
<SoftwarePackage>SystemName</SoftwarePackage>
<Release>1</Release>
<SerialNo>01</SerialNo>
<DateTime>2018-06-26T11:14:05</DateTime>
<!-- This and the next element only appear in files generated by FIS -->
<ReferenceData>Version5.0, LARS 2017-08-01</ReferenceData>
<ComponentSetVersion>1</ComponentSetVersion>
</Source>
</Header>
<SourceFiles>
<!-- The SourceFiles group only appears in files generated by FIS -->
<SourceFile>
<SourceFileName>ILR-LLLLLLLL1819-20180626-144401-01.xml</SourceFileName>
<FilePreparationDate>2018-06-26</FilePreparationDate>
<SoftwareSupplier>Software Systems Inc.</SoftwareSupplier>
<SoftwarePackage>GreatStuffMIS</SoftwarePackage>
<Release>1</Release>
<SerialNo>01</SerialNo>
<DateTime>2018-06-26T11:14:05</DateTime>
</SourceFile>
</SourceFiles>
<LearningProvider>
<UKPRN>99999999</UKPRN>
</LearningProvider>
<!-- 16 yr old learner undertaking full time 16-19 (excluding apprenticeships) funded programme -->
<Learner>
<LearnRefNumber>16Learner</LearnRefNumber>
<PMUKPRN>87654321</PMUKPRN>
<CampId>1234ABCD</CampId>
<ULN>1061484016</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1999-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<PlanLearnHours>440</PlanLearnHours>
<PlanEEPHours>100</PlanEEPHours>
<MathGrade>NONE</MathGrade>
<EngGrade>D</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EDF</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>MCF</LearnFAMType>
<LearnFAMCode>3</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>FME</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>PPE</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record is not required for full time 16-19 (excluding apprenticeships) funded learners -->
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>cb5f0d25-cff4-4ea0-92f5-99378cce306d</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50023408</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-02-14</LearnStartDate>
<LearnPlanEndDate>2016-07-15</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>3</CompStatus>
<LearnActEndDate>2015-04-01</LearnActEndDate>
<WithdrawReason>98</WithdrawReason>
<Outcome>3</Outcome>
<SWSupAimId>c243182a-30af-4879-8f68-3eac708e6bb3</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
</Learner>
<!-- 18 yr old learner undertaking a part-time 16-19 (excluding apprenticeships) funded programme -->
<Learner>
<LearnRefNumber>18Learner</LearnRefNumber>
<CampId>1234ABCD</CampId>
<ULN>1061484466</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1997-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<ALSCost>19000</ALSCost>
<PlanLearnHours>340</PlanLearnHours>
<PlanEEPHours>5</PlanEEPHours>
<MathGrade>C</MathGrade>
<EngGrade>C</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>HNS</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EHC</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record required for part-time 16-19 (excluding apprenticeships) funded learning -->
<LearnerEmploymentStatus>
<EmpStat>10</EmpStat>
<DateEmpStatApp>2015-05-13</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>EII</ESMType>
<ESMCode>3</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50023494</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-05-14</LearnStartDate>
<LearnPlanEndDate>2016-09-30</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>baf7117c-2417-4ee2-8772-4136f2fac383</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>3213a395-9457-4f2b-9d86-20af2363ca02</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
</Learner>
在每个学习者标签的底部,我都用**标签突出显示了我希望输出的内容。
<Learner>
<LearnRefNumber>16Learner</LearnRefNumber>
<PMUKPRN>87654321</PMUKPRN>
<CampId>1234ABCD</CampId>
<ULN>1061484016</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1999-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<PlanLearnHours>440</PlanLearnHours>
<PlanEEPHours>100</PlanEEPHours>
<MathGrade>NONE</MathGrade>
<EngGrade>D</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EDF</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>MCF</LearnFAMType>
<LearnFAMCode>3</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>FME</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>PPE</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record is not required for full time 16-19 (excluding apprenticeships) funded learners -->
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>cb5f0d25-cff4-4ea0-92f5-99378cce306d</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50023408</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-02-14</LearnStartDate>
<LearnPlanEndDate>2016-07-15</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>3</CompStatus>
<LearnActEndDate>2015-04-01</LearnActEndDate>
<WithdrawReason>98</WithdrawReason>
<Outcome>3</Outcome>
<SWSupAimId>c243182a-30af-4879-8f68-3eac708e6bb3</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
**<DB-RU> NULL </DB-RU>**
**<LAD-RU> NULL </LAD-RU>**
**<LAW-RU> NULL </LAW-RU>**
</Learner>
<!-- 18 yr old learner undertaking a part-time 16-19 (excluding apprenticeships) funded programme -->
<Learner>
<LearnRefNumber>18Learner</LearnRefNumber>
<CampId>1234ABCD</CampId>
<ULN>1061484466</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1997-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<ALSCost>19000</ALSCost>
<PlanLearnHours>340</PlanLearnHours>
<PlanEEPHours>5</PlanEEPHours>
<MathGrade>C</MathGrade>
<EngGrade>C</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>HNS</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EHC</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record required for part-time 16-19 (excluding apprenticeships) funded learning -->
<LearnerEmploymentStatus>
<EmpStat>10</EmpStat>
<DateEmpStatApp>2015-05-13</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>EII</ESMType>
<ESMCode>3</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50023494</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-05-14</LearnStartDate>
<LearnPlanEndDate>2016-09-30</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>baf7117c-2417-4ee2-8772-4136f2fac383</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>3213a395-9457-4f2b-9d86-20af2363ca02</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
**<DB-RU> NULL </DB-RU>**
**<LAD-RU> NULL </LAD-RU>**
**<LAW-RU> NULL </LAW-RU>**
</Learner>
答案 0 :(得分:1)
我不确定您是否要照看。首先,您需要找到要插入标签的元素。这是代码。
from bs4 import BeautifulSoup
data='''<Learner>
<LearnRefNumber>16Learner</LearnRefNumber>
<PMUKPRN>87654321</PMUKPRN>
<CampId>1234ABCD</CampId>
<ULN>1061484016</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1999-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<PlanLearnHours>440</PlanLearnHours>
<PlanEEPHours>100</PlanEEPHours>
<MathGrade>NONE</MathGrade>
<EngGrade>D</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EDF</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>MCF</LearnFAMType>
<LearnFAMCode>3</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>FME</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>PPE</LearnFAMType>
<LearnFAMCode>2</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record is not required for full time 16-19 (excluding apprenticeships) funded learners -->
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>cb5f0d25-cff4-4ea0-92f5-99378cce306d</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50023408</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-02-14</LearnStartDate>
<LearnPlanEndDate>2016-07-15</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>3</CompStatus>
<LearnActEndDate>2015-04-01</LearnActEndDate>
<WithdrawReason>98</WithdrawReason>
<Outcome>3</Outcome>
<SWSupAimId>c243182a-30af-4879-8f68-3eac708e6bb3</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
</Learner>
<!-- 18 yr old learner undertaking a part-time 16-19 (excluding apprenticeships) funded programme -->
<Learner>
<LearnRefNumber>18Learner</LearnRefNumber>
<CampId>1234ABCD</CampId>
<ULN>1061484466</ULN>
<FamilyName>Smith</FamilyName>
<GivenNames>Jane</GivenNames>
<DateOfBirth>1997-02-27</DateOfBirth>
<Ethnicity>31</Ethnicity>
<Sex>F</Sex>
<LLDDHealthProb>2</LLDDHealthProb>
<Accom>5</Accom>
<ALSCost>19000</ALSCost>
<PlanLearnHours>340</PlanLearnHours>
<PlanEEPHours>5</PlanEEPHours>
<MathGrade>C</MathGrade>
<EngGrade>C</EngGrade>
<PostcodePrior>BR1 7SS</PostcodePrior>
<Postcode>BR1 7SS</Postcode>
<AddLine1>The Street</AddLine1>
<AddLine2>ToyTown</AddLine2>
<LearnerFAM>
<LearnFAMType>HNS</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>EHC</LearnFAMType>
<LearnFAMCode>1</LearnFAMCode>
</LearnerFAM>
<LearnerFAM>
<LearnFAMType>LSR</LearnFAMType>
<LearnFAMCode>55</LearnFAMCode>
</LearnerFAM>
<!-- Employment status record required for part-time 16-19 (excluding apprenticeships) funded learning -->
<LearnerEmploymentStatus>
<EmpStat>10</EmpStat>
<DateEmpStatApp>2015-05-13</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>EII</ESMType>
<ESMCode>3</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
<!-- 16-19 (excluding apprenticeships) funded study programme -->
<LearningDelivery>
<LearnAimRef>50023494</LearnAimRef>
<AimType>4</AimType>
<AimSeqNumber>1</AimSeqNumber>
<LearnStartDate>2015-05-14</LearnStartDate>
<LearnPlanEndDate>2016-09-30</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR2 7UP</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>baf7117c-2417-4ee2-8772-4136f2fac383</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
<LearningDelivery>
<LearnAimRef>50022246</LearnAimRef>
<AimType>5</AimType>
<AimSeqNumber>2</AimSeqNumber>
<LearnStartDate>2015-09-14</LearnStartDate>
<LearnPlanEndDate>2016-07-02</LearnPlanEndDate>
<FundModel>25</FundModel>
<DelLocPostCode>BR1 3RL</DelLocPostCode>
<CompStatus>1</CompStatus>
<SWSupAimId>3213a395-9457-4f2b-9d86-20af2363ca02</SWSupAimId>
<LearningDeliveryFAM>
<LearnDelFAMType>SOF</LearnDelFAMType>
<LearnDelFAMCode>107</LearnDelFAMCode>
</LearningDeliveryFAM>
</LearningDelivery>
</Learner>'''
soup=BeautifulSoup(data,'lxml')
for item in soup.select('learner'):
finaltag=item.select('LearningDelivery')[-1]
new_tag = soup.new_tag("law-ru")
new_tag.append("NULL")
finaltag.insert_after(new_tag)
new_tag = soup.new_tag("lad-ru")
new_tag.append("NULL")
finaltag.insert_after(new_tag)
new_tag = soup.new_tag("db-ru")
new_tag.append("NULL")
finaltag.insert_after(new_tag)
print(soup)