Wrote most of the script
This commit is contained in:
126
BudaPortaXmlConvert.py
Normal file
126
BudaPortaXmlConvert.py
Normal file
@@ -0,0 +1,126 @@
|
||||
# BudaPortaXmlConvert
|
||||
# Version: 1.0
|
||||
# License: GNU LGPLv3
|
||||
# Maintainer: Peter Gyetvai - gyetpet@mailbox.org
|
||||
# Repo: https://git.gyetvaipeter.hu/infeeeee/budaporta-xml
|
||||
|
||||
# ---------------------------------- import ---------------------------------- #
|
||||
|
||||
import sys
|
||||
import xml.etree.ElementTree as ET
|
||||
import csv
|
||||
import datetime
|
||||
import os
|
||||
|
||||
# --------------------------------- Read arg --------------------------------- #
|
||||
if len(sys.argv) != 2:
|
||||
raise ValueError('Fájl hiányzik!')
|
||||
csvPath = sys.argv[1]
|
||||
print(f'Fájl olvasása: {csvPath}')
|
||||
folder = os.path.dirname(csvPath)
|
||||
|
||||
|
||||
# --------------------------------- Read csv --------------------------------- #
|
||||
|
||||
csvArr = []
|
||||
if os.path.isdir(csvPath):
|
||||
os.listdir(csvPath)
|
||||
else:
|
||||
with open(csvPath, newline='') as csvfile:
|
||||
incsv = csv.reader(csvfile, delimiter=';')
|
||||
for row in incsv:
|
||||
csvArr.append(row)
|
||||
|
||||
|
||||
# ---------------------------- Find column numbers --------------------------- #
|
||||
|
||||
def findColumn(text, array):
|
||||
for i, elem in enumerate(array):
|
||||
if elem == text:
|
||||
return i
|
||||
raise ValueError('Oszlop nem található!' + text)
|
||||
|
||||
|
||||
requiredData = ['Notes', 'Meter n°', 'Reading', 'Reading data']
|
||||
dataColNumbers = []
|
||||
print()
|
||||
for col in requiredData:
|
||||
colnum = findColumn(col, csvArr[0])
|
||||
print(f'"{col}" oszlop sorszáma: {colnum}')
|
||||
dataColNumbers.append(colnum)
|
||||
|
||||
|
||||
# print(dataColNumbers)
|
||||
|
||||
# ----------------------------- Get required data ---------------------------- #
|
||||
|
||||
outData = []
|
||||
csvArr.pop(0)
|
||||
for row in csvArr:
|
||||
dict1 = {}
|
||||
for i, reqCol in enumerate(dataColNumbers):
|
||||
dict1[requiredData[i]] = row[reqCol]
|
||||
outData.append(dict1)
|
||||
|
||||
# print(outData)
|
||||
|
||||
# --------------------- Convert data to required formats --------------------- #
|
||||
|
||||
xmlData = []
|
||||
for dict in outData:
|
||||
theDate = datetime.datetime.strptime(
|
||||
dict["Reading data"], '%d. %m. %Y %H:%M:%S')
|
||||
xmlDict = {
|
||||
"KeszulekAzon": dict["Notes"],
|
||||
"Gyariszam": dict["Meter n°"],
|
||||
"MeroAllas": dict["Reading"].replace(',', '.').replace(' ', ''),
|
||||
"MertekEgyseg": "m3",
|
||||
"LeoMod": "20",
|
||||
"LeoMegjegyzes": "06",
|
||||
"TenyLeoDatum": theDate.strftime('%Y%m%d'),
|
||||
"TenyLeoIdo": theDate.strftime('%H%M%S')
|
||||
}
|
||||
xmlData.append(xmlDict)
|
||||
|
||||
# -------------------------------- Check data -------------------------------- #
|
||||
|
||||
print()
|
||||
print('Adatok ellenőrzése')
|
||||
for mérő in xmlData:
|
||||
for adat in mérő:
|
||||
if len(mérő[adat]) == 0:
|
||||
print(f'Adat hiányzik: "{adat}" a következő sorból:')
|
||||
print(mérő)
|
||||
print()
|
||||
|
||||
# ----------------------------- Generate filename ---------------------------- #
|
||||
|
||||
now = datetime.datetime.now()
|
||||
fileNameBase = 'SZLA_' + now.strftime('%Y%m%d%H%M%S') + '_'
|
||||
fileNameNotFound = True
|
||||
fileNum = 1
|
||||
while fileNameNotFound:
|
||||
fileName = os.path.join(folder, fileNameBase +
|
||||
str(fileNum).zfill(2) + '.XML')
|
||||
fileNum = fileNum+1
|
||||
if not os.path.exists(fileName):
|
||||
fileNameNotFound = False
|
||||
|
||||
|
||||
print(f'Fájl mentése ide: {fileName}')
|
||||
print()
|
||||
|
||||
# --------------------------------- Write xml -------------------------------- #
|
||||
|
||||
data = ET.Element('Leolvasasok')
|
||||
for mérő in xmlData:
|
||||
currSub = ET.SubElement(data, 'Leolvasas')
|
||||
for adat in mérő:
|
||||
a = ET.SubElement(currSub, adat)
|
||||
a.text = mérő[adat]
|
||||
|
||||
print(ET.dump(data))
|
||||
print()
|
||||
|
||||
tree = ET.ElementTree(data)
|
||||
tree.write(fileName)
|
||||
Reference in New Issue
Block a user