I got XML file with structure like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE raml SYSTEM 'raml20.dtd'>
<raml version="2.0" xmlns="raml20.xsd">
<cmData type="actual">
<header>
<log dateTime="2017-04-26T12:03:30" action="created" appInfo="ActualExporter">UIValues are used</log>
</header>
After doing my job, I need to save pandas dataframe to Excel file (.csv or .xlsx), but filename needs to be "log dateTime".
So desired output filename would be: 2017-04-26T12:03:30.xlsx or 2017-04-26T12:03:30.csv.
Anyone have any idea?
Related
I'm trying to parse data collected from a Checkpoint firewall which is in XML format. Ideally, I would like XML data converted to csv or xml format. Once I'm there then I'm good.
Top of XML Text
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="../network_objects.xsl"?>
Code I'm trying, this code I got from other posts.
from lxml import etree
file = "network_objects.xml"
parser = etree.XMLParser(encoding = "iso-8859-1")
tree = etree.parse(file, parser)
Error I'm getting. I don't know anything about XML and what CData is. Can someone provide some guidance?
lxml.etree.XMLSyntaxError: CData section not finished
Petroleum Traders public tunnel IP뽨Ä뻼, line 6787, column 651
I want to modify an existing xml file.
The layout of the existing file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
After i modified a field in the xml i want to get the new xml file, but the modified file is different from the original.
<?xml version='1.0' encoding='UTF-8'?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
Whats the difference between the files:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //this is missing in the mutated file
So what i did:
ET.register_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
ET.register_namespace('', "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03")
#parse the data
tree = ET.parse(self.sepa_xml.path)
root = tree.getroot()
#add a subelement
body = ET.SubElement(root, "{http://www.w3.org/2001/XMLSchema-instance}")
The finale result:
<?xml version='1.0' encoding='UTF-8'?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
<PmtInf>
<PmtInfId>20220929085842-36645</PmtInfId>
<PmtMtd>TRF</PmtMtd>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
<CtgyPurp>
<Cd>SALA</Cd>
</CtgyPurp>
</PmtTpInf>
<ReqdExctnDt>2022-09-29</ReqdExctnDt>
<Dbtr>
<Nm>test name</Nm>
</Dbtr>
</DbtrAgt>
<ChrgBr>SLEV</ChrgBr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>20220929085842-36645/1</EndToEndId>
</PmtId>
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
<xsi: /></Document>. // how can i delete this xsi tag ?
The problem is now that is get an extra tag at the end of the xml file:
<xsi: />. I assume this is because i added a subelement. How can ik delete this last tag ?
I am using python 3.10.4 . I am new at parsing xml files.
like for eg, let the xml file be with the filename "test.xml":
<?xml version="1.0" encoding="UTF-8"?>
<tag1 name="1">
<tag2 name="a"></tag2>
</tag1>
<tag1 name = "2">
<tag2 name = "b"></tag2>
</tag1>
</xml>
python code
import xml.dom.minidom
file = xml.dom.minidom.parse('test.xml')
list = []
tags=file.getElementsByTagName("tag1")
for tag in tags:
if(tag.getAttribute("name")=="1"):
print(tag.getAttribute("tag2"))
So here I want to access the tag2 of tag1 with name="1". How can I do it?
Haven't done this kind of process in xml before.
I have these empty folders, called: 125,127,128
and I have this xml:
<?xml version="1.0" encoding="ASCII"?>
<Metadata version="1.0">
<CODE_OK>510</CODE_OK>
<DeliveryDate>13/08/2018</DeliveryDate>
I want to replace the number between:<CODE_OK>510</CODE_OK> with the number that is each folder's name:125,127 and 128 and drop each new xml in the corresponding folder.
This is one approach.
import xml.etree.ElementTree as ET
import os
sampleXML = """<?xml version="1.0" encoding="ASCII"?>
<Metadata version="1.0">
<CODE_OK>510</CODE_OK>
<DeliveryDate>13/08/2018</DeliveryDate>
</Metadata>
"""
tree = ET.ElementTree(ET.fromstring(sampleXML))
for folder in os.listdir("YourPath"): #Iterate the dir
tree.find("CODE_OK").text = folder #Update dir name in XML
tree.write(open(os.path.join(r"YourPath", folder, "yourxml.xml"), "w")) #Write to XML
i'm using minidom to create an xml file
and I would like to add the header to my file , i.e,
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE root SYSTEM "file.dtd">
how can I add it?