-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV_2_excel.py
195 lines (154 loc) · 6.27 KB
/
CSV_2_excel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Program: Desigo CC 4.xx and 5.0 Trend Data convertor from CSV file into EXCEL files.
Author: Anton Gegeniger
e-Mail: anton.gegeniger@siemens.com
Package requirements:
Python 3.9 +
pandas 1.3.5
numpy 1.19.5
xlwt 1.3.0
"""
import numpy as np
import pandas
import glob
import time
import json
import os
import sys
#Get files in the directory
path = 'c:/trends'
processed = {}
settings = {'TrendPath':'c:/trends','OutputPath':'c:/trends/excel','ScanTime':600}
settings_fn = './Settings.json'
processed_fn = './processed.json'
scanTime = 3
def ReadIntoTable(fileName):
#read csv file into DataFrame
trend_data = pandas.read_csv(fileName, sep = ';', header = 0)
#convert Timestamp into DateTime format
trend_data['DateTime'] = pandas.to_datetime(trend_data['DateTime'])
#drop duplicates in timestamp for the same points
noDup = trend_data.drop_duplicates(subset=['DateTime','Data Source'], keep='first')
#pivotig the table by TimeStamp and point names as rows
pivot = pandas.pivot(noDup,index = 'DateTime',columns = 'Data Source', values = 'Value')
colNames = pivot.columns
#removing columns with NaN data if exist
pivot = pivot.loc[:, pivot.columns.notnull()]
#resampling table to 15 minutes period
pivot = pivot.resample('15min').pad()
#filling gaps with upstream data
pivot.ffill(inplace = True)
#converting numeric columns to Float64
for col in pivot.columns:
pivot[col] = pandas.to_numeric(pivot[col], errors = 'ignore')
#cleaning columns names from 'System:ManagementView.....'
oldNames = []
for col in colNames:
oldNames.append(col)
newNames = CleanName(oldNames)
pivot.columns = newNames
return pivot
def CleanName(names):
newNames = []
for nam in names:
if nam == None or len (str(nam)) < 5: continue
pLeft = str(nam).rfind('.')
sLeft = str(nam)[:pLeft]
pRight = sLeft.rfind('.')+1
sRight = sLeft[pRight:]
newName = sRight
newNames.append(newName)
return newNames
def SaveProcessed():
global processed
global processed_fn
json_process = json.dumps(processed, indent=2)
with open(processed_fn,'w') as f:
f.write(json_process)
#settings = json_process
def ReadProcessed():
json_pro ={}
global processed
global processed_fn
try:
f = open(processed_fn,'r')
json_pro = json.load(f)
processed = json_pro
print ('\033[94m' + 'Processed file has been found!'+'\033[0m')
print ('\033[94m' + str(len(processed)) + ' files counted'+'\033[0m' )
except:
print('\033[93m' + 'WARNING: Processed file is not found, new one will be generated' + '\033[0m')
def ReadSettings():
json_str ={}
global settings
global settings_fn
settings_path = settings_fn
try:
f = open(settings_path,'r')
json_str = json.load(f)
settings = json_str
print ('\033[92m' + '##################### SETINGS ####################'+'\033[0m')
print ('\033[92m' + 'Settings file has been found!'+'\033[0m')
print ('\033[92m' + 'Path to the Trends folder: ' + settings['TrendPath'] +'\033[0m' )
print ('\033[92m' + 'Path to the Excel Output folder: ' + settings['OutputPath'] +'\033[0m' )
print ('\033[92m' + '################### END SETINGS ##################'+'\033[0m')
print()
print()
time.sleep(3)
return True
except:
print ('\033[93m' + '#################### SETINGS ####################'+'\033[0m')
print('\033[93m' + 'WARNING: Settings file is not found, default settings will be used' + '\033[0m')
print()
print ('\033[93m' + 'Path to the Trends folder: ' + settings['TrendPath'] +'\033[0m' )
print ('\033[93m' + 'Path to the Excel Output folder: ' + settings['OutputPath'] +'\033[0m' )
print ('\033[93m' + '################### END SETINGS #################'+'\033[0m')
print()
print()
time.sleep(3)
return False
def SaveSettings():
global settings_fn
global settings
json_process = json.dumps(settings, indent=2)
with open(settings_fn,'w') as f:
f.write(json_process)
def CheckFolder(folder_path,createNew):
if not os.path.isdir(folder_path):
if createNew:
print ('\033[93m' + '!!! WARNING: The folder ' + folder_path + ' is NOT exist and will be created.'+'\033[0m')
os.makedirs(folder_path)
else:
print ('\033[93m' + '!!! WARNING: The folder ' + folder_path + ' is NOT exist!'+'\033[0m')
else:
print ('\033[93m' + folder_path + ' The folder is OK' +'\033[0m')
# ===================== Main Program ========================
os.chdir(os.path.dirname(sys.argv[0]))
if not ReadSettings():
SaveSettings()
scanTime = settings['ScanTime']
#check folders defined in the settings, if not exist create new one
print ('\033[92m' + 'Check if Trend folders defined in the Settings is existing'+'\033[0m')
CheckFolder (settings['TrendPath'], True)
print ('\033[92m' + 'Check if Trend folders defined in the Settings is existing'+'\033[0m')
CheckFolder (settings['OutputPath'], True)
ReadProcessed()
print ('\033[92m' + 'Waiting for new CSV files........'+'\033[0m')
while True:
files = glob.glob(settings['TrendPath'] +'/*.csv')
#Processed needs to be stored in the file, in case the program was restarted, it will not process all files over again
pivot = pandas.DataFrame
for file in files:
if not processed.get(file):
pivot = ReadIntoTable(file)
tr_path = settings['TrendPath']
out_path = settings['OutputPath']
outFileName = file.replace(tr_path,out_path).lower().replace('.csv','.xlsx')
try:
pivot.to_excel(outFileName)
processed[file] = True
SaveProcessed()
print ('\033[92m' + outFileName + " Has been processed!" + '\033[0m')
except:
print ('\033[93m' + outFileName + " Hasn't been saved! Make sure it is not open and folder is not setup as Read Only" + '\033[0m')
time.sleep(scanTime)