csv2db hinzugefügt
This commit is contained in:
parent
580a93d620
commit
093f87b4b2
1 changed files with 44 additions and 0 deletions
44
src/csv2db.py
Normal file
44
src/csv2db.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
'''
|
||||||
|
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)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue