Ich arbeite mit bestimmten Datensätzen, die ich in einer vorhandenen Datei in einem festen Abschnitt einfügen möchte. Ich mag von der Suche, wie dies die XML-Datei ändern:Einfügen von Daten in XML-Datei mit Python
<SBEDataUploadFile>
<ApplicationData>
<firmware>
<SoftwareVersion>1.0</SoftwareVersion>
<BuildDate>Dec 1 2012 10:43:42</BuildDate>
</firmware>
</ApplicationData>
</SBEDataUploadFile>
sehen wie folgt aus:
<SBEDataUploadFile>
<![CDATA[
** Location 001
** Latitude In 18.33885
** Longitude In 64.97647
** Time In 11:55
** Depth (ft) 10
** Line Out (ft) 5
** Time Out 11:56
** Latitude Out 18.33885
** Longitude Out 64.97647
** Notes
]]>
<ApplicationData>
<firmware>
<SoftwareVersion>1.0</SoftwareVersion>
<BuildDate>Dec 1 2012 10:43:42</BuildDate>
</firmware>
</ApplicationData>
</SBEDataUploadFile>
Ich versuchte dies mit xml.etree.ElementTree
, aber mein Ergebnis ist, dass es um den Kommentar zu Boden anhängt nach die </ApplicantionData>
. Hier ist meine aktuellen Code:
import xml.etree.ElementTree as ET
#variables
location = "BPT"
latitude_in = 0
longitude_in = 0
time_in = 0
depth = 0
line_out = 0
time_out = 0
latitude_out = 0
longitude_out = 0
notes = ""
#formatting and converting all variables to string
toString = "<![CDATA["+"\n"+"** Location "+location+"\n"+"** Latitude In "\
+str(latitude_in)+"\n"+"** Longitude In "+str(longitude_in)+"\n"+\
"** Time In "+str(time_in)+"\n"+"** Depth (ft) "+str(depth)+"\n"+"** Line Out (ft) "\
+str(line_out)+"\n"+"** Time Out "+str(time_out)+"\n"+"** Latitude Out "\
+str(latitude_out)+"\n"+"** Longitude Out "+str(longitude_out)+"\n"+"** Notes "+notes+"\n"+"]]>"
xml_filepath = xmlfilepath.xml
xml_tree = ET.parse(xml_filepath)
xml_root = xml_tree.getroot()
ET.SubElement(xml_root, toString)
print ET.tostring(xml_root)
und das sind meine aktuellen Ergebnisse:
<SBEDataUploadFile>
<ApplicationData>
<firmware>
<SoftwareVersion>1.0</SoftwareVersion>
<BuildDate>Dec 1 2012 10:43:42</BuildDate>
</firmware>
</ApplicationData>
<<![CDATA[
** Location BPT
** Latitude In 0
** Longitude In 0
** Time In 0
** Depth (ft) 0
** Line Out (ft) 0
** Time Out 0
** Latitude Out 0
** Longitude Out 0
** Notes
]]> /></SBEDataUploadFile>
Ich möchte diese in der Lage sein, um es wie meine gewünschten Ergebnisse aussehen und loszuwerden, dass zusätzliche />
direkt vor dem </SBEDataUploadFile>
.
Vielen Dank !!!, Ich liebe nur, wo ich so nah an der Antwort bin, aber keine Zigarre. Vielen Dank!!!. –