pCharger liest alle csv dateien eines ordner ein
This commit is contained in:
parent
093f87b4b2
commit
0db4c49f7b
2 changed files with 53 additions and 44 deletions
|
|
@ -1,44 +0,0 @@
|
|||
'''
|
||||
Created on 16.01.2014
|
||||
|
||||
@author: rauchp
|
||||
'''
|
||||
import csv
|
||||
from datetime import datetime
|
||||
|
||||
def clean(list):
|
||||
for i in range(0, len(list)):
|
||||
list[i] = list[i].strip()
|
||||
list[i] = list[i].strip('\xef\xbb\xbf')
|
||||
return list
|
||||
|
||||
def makeDatetime(time):
|
||||
tmp = []
|
||||
time = time.split(' ')
|
||||
time[0] = time[0].split('.')
|
||||
time[1] = time[1].split(':')
|
||||
tmp.append(int(time[0][2]))
|
||||
tmp.append(int(time[0][1]))
|
||||
tmp.append(int(time[0][0]))
|
||||
tmp.append(int(time[1][0]))
|
||||
tmp.append(int(time[1][1]))
|
||||
tmp.append(int(time[1][2]))
|
||||
|
||||
return datetime(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]).__str__()
|
||||
|
||||
sqlerr = "INSERT INTO %s VALUES (\'%s\',%s,%s)"
|
||||
sqlnor = "INSERT INTO %s VALUES (\'%s\',%s,%s,%s,%s,%s)"
|
||||
|
||||
csv_data = csv.reader(file('test.csv'), delimiter=';')
|
||||
|
||||
for row in csv_data:
|
||||
row = clean(row)
|
||||
row[0] = "P-Charger_%s" % row[0]
|
||||
row[1] = makeDatetime(row[1])
|
||||
row[3] = int(row[3], 16).__str__()
|
||||
if row[2] not in 'SE':
|
||||
row[6] = int(row[6], 16).__str__()
|
||||
print sqlnor % tuple(row)
|
||||
else:
|
||||
print sqlerr % tuple(row)
|
||||
|
||||
53
src/pCharger.py
Normal file
53
src/pCharger.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
'''
|
||||
Created on 16.01.2014
|
||||
|
||||
@author: rauchp
|
||||
'''
|
||||
import csv
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
sqlerr = "INSERT INTO %s (DateTime,Type,Error) VALUES (\'%s\',%s,%s)"
|
||||
sqlnor = "INSERT INTO %s (DateTime,Type,cardnumber,customernumber,plug,meterreading) VALUES (\'%s\',%s,%s,%s,%s,%s)"
|
||||
folder = 'pCharger'
|
||||
|
||||
def clean(list):
|
||||
for i in range(0, len(list)):
|
||||
list[i] = list[i].strip()
|
||||
#list[i] = list[i].strip('\xef\xbb\xbf')
|
||||
return list
|
||||
|
||||
def makeDatetime(time):
|
||||
tmp = []
|
||||
time = time.split(' ')
|
||||
time[0] = time[0].split('.')
|
||||
time[1] = time[1].split(':')
|
||||
tmp.append(int(time[0][2]))
|
||||
tmp.append(int(time[0][1]))
|
||||
tmp.append(int(time[0][0]))
|
||||
tmp.append(int(time[1][0]))
|
||||
tmp.append(int(time[1][1]))
|
||||
tmp.append(int(time[1][2]))
|
||||
|
||||
return datetime(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]).__str__()
|
||||
|
||||
def generateSQL(folder):
|
||||
ordner = os.listdir(folder)
|
||||
for datei in ordner:
|
||||
path = "%s/%s" % (folder, datei)
|
||||
csv_data = csv.reader(file(path), delimiter=';')
|
||||
try:
|
||||
for row in csv_data:
|
||||
row = clean(row)
|
||||
row[0] = "P-Charger_%s" % row[0]
|
||||
row[1] = makeDatetime(row[1])
|
||||
row[3] = int(row[3], 16).__str__()
|
||||
if row[2] not in 'SE':
|
||||
row[6] = int(row[6], 16).__str__()
|
||||
print sqlnor % tuple(row)
|
||||
else:
|
||||
print sqlerr % tuple(row)
|
||||
except:
|
||||
continue
|
||||
|
||||
generateSQL(folder)
|
||||
Loading…
Add table
Add a link
Reference in a new issue