簡體   English   中英

從 Python 中的 XML 文件中刪除指定數據

[英]Remove specified data from an XML file in Python

基本上我要做的是將 xml 文件導入 Python 並刪除 entityNo 為 1111111111 的任何數據。

這是 xml 數據的文本副本:

<memberBasedResearchDataImport>
   <surveyDescr>D520</surveyDescr>
   <surveyType>MEG</surveyType>
   <surveyRequester>1543588274</surveyRequester>
   <product>DISC</product>
   <externalRef>PKG_RPTA88425_4</externalRef>
   <DateTimeCreated>20191019 05:10:33</DateTimeCreated>
   <identifierSettings>
       <identifierType id="1" database="DARE" schema="dp_da_crm" table="ratings" column="object_cd" columnType="number"></identifierType>
       <identifierType id="2" database="DARE" schema="dp_da_ent" table="entity" column="full_name" columnType="varchar2"></identifierType>
       <identifierType id="3" database="dual" schema="dual" table="dual" column="dual" columnType="varchar2"></identifierType>
   </identifierSettings>
   <row id="1" entityNo="1054354679" entityRole="KP" policyNo="0" agentEntityNo="1103354880">
       <templateValue name="INTERACTION_DAY" value="Friday"></templateValue>
       <identifierType id="1" value="671535634817"></identifierType>
       <identifierType id="2" value="CUSTOMER SERVICES: SALES"></identifierType>
   </row>
   <row id="2" entityNo="1111111111" entityRole="AP" policyNo="0" agentEntityNo="11351512571">
       <templateValue name="INTERACTION_DAY" value="Friday"></templateValue>
       <identifierType id="1" value="6715354549"></identifierType>
       <identifierType id="2" value="CUSTOMER SERVICES: ADMIN"></identifierType>
   </row>
   <row id="3" entityNo="100000571" entityRole="LP" policyNo="0" agentEntityNo="112355274">
       <templateValue name="INTERACTION_DAY" value="Friday"></templateValue>
       <identifierType id="1" value="671546864"></identifierType>
       <identifierType id="2" value="CUSTOMER SERVICES: SALES"></identifierType>
   </row>
   <row id="4" entityNo="1111111111" entityRole="HP" policyNo="0" agentEntityNo="112456466850"><templateValue name="INTERACTION_DAY" value="Friday"></templateValue>
       <identifierType id="1" value="6793437110"></identifierType>
       <identifierType id="2" value="CUSTOMER SERVICES: RETURNS"></identifierType>
   </row>
</memberBasedResearchDataImport>

到目前為止,我已經嘗試了一些我在網上找到的解決方案,但沒有成功。 下面的代碼是我在另一篇文章中找到的,但不會刪除我需要它刪除的數據。 我的代碼如下,任何幫助將不勝感激。 同樣,我需要刪除 entityNo = 1111111111 的數據,然后以 xml 格式導出數據。

from xml.etree.ElementTree import ElementTree

path_to_xml_file = "C:\Users\username\Documents\Data_File.xml"

tree = ElementTree()
tree.parse(path_to_xml_file)

foos = tree.findall("entityNo")
for foo in foos:
  bars = foo.find("1111111111")
  for bar in bars:
    foo.remove(bar)

tree.write("C:\Users\username\Documents\Data_File.xml")

給你 go

import xml.etree.ElementTree as ET

path_to_xml_file = "C:\Users\username\Documents\Data_File.xml"


root=ET.parse(path_to_xml_file)

for country in root.findall('row'):
    val_to_delete = country.attrib['entityNo']
    if val_to_delete == 1111111111:
        root.remove(country)

root.write("C:\Users\username\Documents\Data_File.xml")

您的原始代碼中有一些錯誤

  1. 您的導入語句是錯誤的。 請找到我的代碼以進行更正
  2. 您發現屬性必須使用.attrib[] 訪問該屬性,就像在我的片段中一樣
  3. 最重要的是,如果您正在進行任何更新,則在迭代 for 循環時,例如 for like remove 在您的情況下它應該是原始值而不是迭代器 object 即。 在您的代碼中所做的任何更改都應針對 foos 而不是針對 foo。 foo 只是一個副本

希望這可以幫助..

與其嘗試查找所有“entityNo”,不如遍歷行,查看屬性是否為 11111,如果是,則將其刪除。 像這樣的東西:

root = tree.getroot()
for row in root.findall('row'):
    if row.attrib['entityNo'] == "1111111111":
        root.remove(row)

試試這個:

import xml.etree.ElementTree as ET


file = 'C:\Users\username\Documents\Data_File.xml'
case = '1111111111'

element = ET.parse(file)
root = element.getroot()

for child in root:
    if child.attrib.get('entityNo') == case:
        root.remove(child)

element.write(file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM