【问题标题】:Using Python With bs4(Lxml), To edit text inside XML tag将 Python 与 bs4(Lxml) 一起使用,编辑 XML 标记内的文本
【发布时间】:2019-11-19 23:46:35
【问题描述】:

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

我正在尝试从 XML 邮政编码标签中删除最后三个字符以匿名数据。

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

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>

当前代码:

#Importing BS4# 
from bs4 import BeautifulSoup

#Opening Origional XML File, Setting soup to BS# 
with open("ILR_mock_data.xml", "r") as infile:
    xml_text = infile.read()

soup = BeautifulSoup(xml_text, 'xml')




#Postcode (Deleting last 3 digits)#
for postcode_tag in soup.find_all("Postcode"):
    postcode_tag.string[:-3]


with open("SEND_ME_TO_RCU.xml", "w") as outfile:
    outfile.write(soup.prettify())

希望 XML 有

<Postcode>BR1 7SS</Postcode>

新的邮政编码将是

<Postcode>BR1</Postcode>

【问题讨论】:

  • stackoverflow.com/a/41537949/7965340 - 我认为上面的问题已经在这个链接中解决了
  • 我认为您误解了这个问题。我已将其编辑为更具体。还是谢谢
  • postcode_tag.string = postcode_tag.string[:-3]?

标签: python xml beautifulsoup lxml


【解决方案1】:

修复了使用

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

【讨论】:

    【解决方案2】:

    以下代码使用 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)
    

    输出

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

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2010-09-05
      • 1970-01-01
      • 2018-03-20
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      相关资源
      最近更新 更多