Я хочу использовать xml.etree.ElmentTree, чтобы разобрать xml, чтобы изменить значение, а затем записать в новый файл, но если тег с текстовым значением None, после того, как я запишу в новый xml, правильный тег будет потерян:
Ниже приведено оригинальное содержание:
<LignesReception>
<CodeBarre>3607171442485</CodeBarre>
<QteRecue>8</QteRecue>
<QteRecue1Choix>8</QteRecue1Choix>
<QteRecue2Choix>0</QteRecue2Choix>
<DateLivraisonPrevue>14022019</DateLivraisonPrevue>
<QteRetourneeFourn></QteRetourneeFourn>
<CodeCauseRetourFourn></CodeCauseRetourFourn>
<PxAchatInitial></PxAchatInitial>
<PxAchatModifie></PxAchatModifie>
<QteAnnoncee>8</QteAnnoncee>
<QteEnEchantillon></QteEnEchantillon>
<QteAttDecision>0</QteAttDecision>
</LignesReception>
Удар - это содержимое после того, как я записываю в новый XML-файл:
<LignesReception>
<CodeBarre>3607171442485</CodeBarre>
<QteRecue>8</QteRecue>
<QteRecue1Choix>8</QteRecue1Choix>
<QteRecue2Choix>0</QteRecue2Choix>
<DateLivraisonPrevue>27022019</DateLivraisonPrevue>
<QteRetourneeFourn />
<CodeCauseRetourFourn />
<PxAchatInitial />
<PxAchatModifie />
<QteAnnoncee>8</QteAnnoncee>
<QteEnEchantillon />
<QteAttDecision>0</QteAttDecision>
</LignesReception>
Вот мой сценарий:
import os
from xml.etree import ElementTree as ET
for file in os.listdir("."):
if file.endswith("xml"):
tree = ET.parse(file)
for i in tree.iter():
if i.text is None:
i.text = ""
if i.tag == "DateLivraisonPrevue":
i.text = "27022019"
tree.write("New xml.xml",encoding="UTF-8",xml_declaration=True)
Может кто-нибудь помочь в этом вопросе исправить запись в формате xml, спасибо!