Wrote most of the script

This commit is contained in:
2021-06-02 00:14:36 +02:00
parent c3698b4833
commit 19f1ac487a
4 changed files with 166 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
example
example-csv

11
BudaPortaXmlConvert.bat Normal file
View File

@@ -0,0 +1,11 @@
@echo off
if exist %1\* (
for /R "%1" %%f in (*.csv) do ( python %~dp0\BudaPortaXmlConvert.py "%%f")
@REM for /f %%a IN ('dir /b /s %1') do python %~dp0\BudaPortaXmlConvert.py "%%a"
@REM echo Directory
) else (
python %~dp0\BudaPortaXmlConvert.py %1
)
pause

126
BudaPortaXmlConvert.py Normal file
View 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)

View File

@@ -1,2 +1,29 @@
# budaporta-xml
Vízműveknek megfelelő xml konvertálása leolvasó programból kijövő csv fájlból.
## Adatok megfeleltetése
| Fix érték | csv | xml |
| --------- | ------------ | ------------- |
| | Notes | KeszulekAzon |
| | Meter n° | Gyariszam |
| | Reading | MeroAllas |
| m3 | | MertekEgyseg |
| 20 | | LeoMod |
| 06 | | LeoMegjegyzes |
| | Reading data | TenyLeoDatum |
| | Reading data | TenyLeoIdo |
### Egyéb adatok
#### Fájlnév
File neve: SZLA_EEEEHHNNOOPPMM_SS.XML (pl. SZLA_20121101122110_01.XML)
(EEEE-év, HH-hó, NN-nap, OO-óra, PP-perc, MM-másodperc, SS-sorszám(pl. 01, 02))
Egy xml fájl neve hossza legfeljebb 40 karakter lehet!
A file nevében ne legyen ékezetes karakter.
Egy sor sem lehet 50 karakternél hosszabb!