From 4405a19633789dd44a8fa5350c672a93ab485762 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Tue, 31 Aug 2021 00:00:12 +0200 Subject: [PATCH] Prettify xml output --- BudaPortaXmlConvert.py | 24 ++++++++++++++++-------- requirements.txt | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 requirements.txt diff --git a/BudaPortaXmlConvert.py b/BudaPortaXmlConvert.py index 3c1ea83..bf5f093 100644 --- a/BudaPortaXmlConvert.py +++ b/BudaPortaXmlConvert.py @@ -11,6 +11,7 @@ import xml.etree.ElementTree as ET import csv import datetime import os +from bs4 import BeautifulSoup # ---------------------------------------------------------------------------- # @@ -182,24 +183,31 @@ while fileNameNotFound: if not os.path.exists(fileName): fileNameNotFound = False - +print() print(f'Fájl mentése ide: {fileName}') print() -# --------------------------------- Write xml -------------------------------- # +# ------------------------------- Generate xml ------------------------------- # data = ET.Element('Leolvasasok') -data.tail = '\r\n' for mérő in allXmlData: currSub = ET.SubElement(data, 'Leolvasas') - currSub.tail = '\r\n' for adat in mérő: a = ET.SubElement(currSub, adat) a.text = mérő[adat] - a.tail = '\r\n' -print(ET.dump(data)) +xmlString = ET.tostring(data) + +# ------------------------------- Prettify xml ------------------------------- # + +bs_data = BeautifulSoup(xmlString, 'xml') +prettyXml = bs_data.prettify() + +print(prettyXml) print() -tree = ET.ElementTree(data) -tree.write(fileName, encoding="utf-8", xml_declaration=True) +# --------------------------------- Save file -------------------------------- # + +f = open(fileName, "x") +f.write(prettyXml) +f.close \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9319064 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +beautifulsoup4 +lxml \ No newline at end of file