@@ -78,16 +78,13 @@ def read_csv(path_to_csv):
7878 read_data = []
7979
8080 for row in data :
81- #if "UNKNOWN" in row and not "UNKNOWN" in fieldnames:
82- # fieldnames.append("UNKNOWN")
8381 row ["dm_source_file" ] = os .path .split (path_to_csv )[1 ]
8482 read_data .append (row )
8583
8684 return (fieldnames , read_data )
8785
8886
89-
90- def write_csv (path_to_csv , header , data ):
87+ def write_csv (path_to_csv , header , data , ui = None , files = None ):
9188 """
9289 Writes a csv file from the specified data
9390
@@ -106,16 +103,32 @@ def write_csv(path_to_csv, header, data):
106103 print >> sys .stderr , e
107104 return False
108105
106+ # Sort header alphabetically
107+ header .sort ()
108+
109109 output = csv .DictWriter (out_fp , fieldnames = header , dialect = 'excel' , lineterminator = "\n " , quotechar = "\" " , restval = "" ,escapechar = "\\ " )
110110 output .writeheader ()
111111
112112 errorCount = 0
113+ counter = 0
114+ if ui and files :
115+ rows_part = len (data )/ files
116+
113117 for row in data :
118+ counter += 1
114119 try :
115120 output .writerow (row )
116121 except Exception as e :
117122 print >> sys .stderr , "Warning (" + row ["dm_source_file" ] + "): " + str (e )
118- errorCount += 1
123+ errorCount += 1
124+
125+ if not ui is None :
126+ if counter % rows_part == 0 :
127+ part = counter / rows_part
128+ progress = int ((part + files )/ float (2 * files + 1 )* 100 )
129+ ui .progressBar .setValue (progress )
130+
131+
119132 return errorCount
120133
121134def read_xls (path_to_xls ):
@@ -146,7 +159,7 @@ def read_xls(path_to_xls):
146159
147160 return (headers , data )
148161
149- def write_xls (path_to_xls , header , data ):
162+ def write_xls (path_to_xls , header , data , ui = None , files = None ):
150163 """
151164 Writes a xls (Excel 97-2003) file from the specified data
152165
@@ -159,6 +172,8 @@ def write_xls(path_to_xls, header, data):
159172 Returns:
160173 errorCount (int): 0 if no errors, or otherwise the number of (non-critical) errors that occurred
161174 """
175+ # Sort header alphabetically
176+ header .sort ()
162177
163178 workbook = xlwt .Workbook (encoding = 'utf8' )
164179 worksheet = workbook .add_sheet ("merged_data" )
@@ -171,9 +186,14 @@ def write_xls(path_to_xls, header, data):
171186 style .font = font # Apply the Font to the Style
172187 for col in xrange (len (header )):
173188 worksheet .write (0 ,col ,header [col ],style )
189+
190+ counter = 0
191+ if ui and files :
192+ rows_part = len (data )/ files
174193
175194 # Write data
176- for row in xrange (0 ,len (data )):
195+ for row in xrange (0 ,len (data )):
196+ counter += 1
177197 for col in xrange (len (header )):
178198 col_name = header [col ]
179199 try :
@@ -182,12 +202,18 @@ def write_xls(path_to_xls, header, data):
182202 value = ""
183203
184204 worksheet .write (row + 1 , col , correct_datatype (value ) )
205+
206+ if not ui is None :
207+ if counter % rows_part == 0 :
208+ part = counter / rows_part
209+ progress = int ((part + files )/ float (2 * files + 1 )* 100 )
210+ ui .progressBar .setValue (progress )
185211
186212 workbook .save (path_to_xls )
187213 return 0
188214
189215
190- def write_xlsx (path_to_xlsx , header , data ):
216+ def write_xlsx (path_to_xlsx , header , data , ui = None , files = None ):
191217 """
192218 Writes a xlsx file (Excel 2010 and higher) from the specified data
193219
@@ -205,20 +231,28 @@ def write_xlsx(path_to_xlsx, header, data):
205231 # elegant way.
206232 if Workbook is None :
207233 return 1
234+
235+ # Sort header alphabetically
236+ header .sort ()
208237
209238 workbook = Workbook ()
210239 worksheet = workbook .worksheets [0 ]
211240
212241 worksheet .title = "merged_data"
213-
242+
214243 # Write header in bold
215244 for col in xrange (len (header )):
216245 cell = worksheet .cell (row = 0 ,column = col )
217246 cell .value = header [col ]
218247 cell .style .font .bold = True
219-
248+
249+ counter = 0
250+ if ui and files :
251+ rows_part = len (data )/ files
252+
220253 # Write data
221- for row in xrange (0 ,len (data )):
254+ for row in xrange (0 ,len (data )):
255+ counter += 1
222256 for col in xrange (len (header )):
223257 col_name = header [col ]
224258 try :
@@ -227,6 +261,12 @@ def write_xlsx(path_to_xlsx, header, data):
227261 value = ""
228262
229263 worksheet .cell (row = row + 1 ,column = col ).value = correct_datatype (value )
264+
265+ if not ui is None :
266+ if counter % rows_part == 0 :
267+ part = counter / rows_part
268+ progress = int ((part + files )/ float (2 * files + 1 )* 100 )
269+ ui .progressBar .setValue (progress )
230270
231271 workbook .save (filename = str (path_to_xlsx ))
232272 return 0
@@ -274,7 +314,7 @@ def mergeFolder(folder, destination, ui=None):
274314 counter += 1
275315
276316 if not ui is None :
277- progress = int (counter / float (len (valid_files )+ 1 )* 100 )
317+ progress = int (counter / float (2 * len (valid_files )+ 1 )* 100 )
278318 ui .progressBar .setValue (progress )
279319
280320 print "Writing merged data to file (please be patient as this can take a while...)"
@@ -291,11 +331,11 @@ def mergeFolder(folder, destination, ui=None):
291331 destination_ext = os .path .splitext (str (destination ))[1 ]
292332
293333 if destination_ext == ".csv" :
294- errorCount = write_csv (destination , col_names , total_data )
334+ errorCount = write_csv (destination , col_names , total_data , ui , len ( valid_files ) )
295335 elif destination_ext == ".xls" :
296- errorCount = write_xls (destination , col_names , total_data )
336+ errorCount = write_xls (destination , col_names , total_data , ui , len ( valid_files ) )
297337 elif destination_ext == ".xlsx" :
298- errorCount = write_xlsx (destination , col_names , total_data )
338+ errorCount = write_xlsx (destination , col_names , total_data , ui , len ( valid_files ) )
299339
300340 if not ui is None :
301341 ui .progressBar .setValue (100 )
0 commit comments