使用Python和bs4(Lxml)编辑XML标记内的文本

使用Python和bs4(Lxml)编辑XML标记内的文本,python,xml,beautifulsoup,lxml,Python,Xml,Beautifulsoup,Lxml,我对python、BS4和Lxml解析器都是新手 我试图从XML邮政编码标记中删除最后三个字符,以匿名化数据 当前代码运行良好,没有任何错误,但最后三位数字没有从输出的XML文件中删除 XML模拟数据- <?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 loa

我对python、BS4和Lxml解析器都是新手

我试图从XML邮政编码标记中删除最后三个字符,以匿名化数据

当前代码运行良好,没有任何错误,但最后三位数字没有从输出的XML文件中删除

XML模拟数据-

<?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>
希望XML已经实现了这一点

<Postcode>BR1 7SS</Postcode>
BR1 7SS
新的邮政编码将是

<Postcode>BR1</Postcode>
BR1

使用修复了该问题

for pripostcode_tag in soup.find_all("PostcodePrior"):   
    pripostcode_tag.string = pripostcode_tag.string[:-3]

下面的代码使用了xml的简化版本(,但也应该与OP的xml一起使用)。它不使用任何外部库

import xml.etree.ElementTree as ET

xml_sample = '''<r><Postcode>ACBDEF</Postcode></r>'''
root = ET.fromstring(xml_sample)
post_codes = root.findall('.//Postcode')
for pc in post_codes:
  pc.text = pc.text[:-3]
ET.dump(root)
将xml.etree.ElementTree作为ET导入
xml_sample=''ACBDEF''
root=ET.fromstring(xml\u示例)
post_code=root.findall('.//Postcode')
对于post_代码中的pc:
pc.text=pc.text[:-3]
ET.dump(根目录)
输出

<r><Postcode>ACB</Postcode></r>
ACB

-我认为上述问题已在本链接中得到解决。我认为您误解了这个问题。我对它进行了编辑,使之更加具体。无论如何,谢谢你
postcode\u tag.string=postcode\u tag.string[:-3]
<r><Postcode>ACB</Postcode></r>