@@ -66,22 +66,32 @@ def read_csv(path_to_csv):
6666 print >> sys .stderr , e
6767 return False
6868
69+ # Try to determine which format of csv is used. In some countries, the
70+ # list separator symbol is ; instead of , and hopefully this will catch that
71+ # discrepancy.
6972 try :
7073 dialect = csv .Sniffer ().sniff (f_csv .readline ())
7174 except :
72- print >> sys .stderr , "Failed to sniff file parameters , assuming , as delimiter symbol"
75+ print >> sys .stderr , "Failed to sniff parameters for file {0} , assuming , to be the delimiter symbol" . format ( os . path . split ( path_to_csv )[ 1 ])
7376 dialect = csv .get_dialect ('excel' )
74-
77+
7578 f_csv .seek (0 )
76- data = csv .DictReader (f_csv ,dialect = dialect ,restkey = "UNKNOWN" ,restval = "" )
77- fieldnames = data .fieldnames
78- read_data = []
7979
80- for row in data :
81- row ["dm_source_file" ] = os .path .split (path_to_csv )[1 ]
82- read_data .append (row )
80+ # Read the file with dictreader. If the file is empty or corrupt (such as defaultlog.csv in OpenSesame)
81+ # then skip the file but print an error message.
82+ try :
83+ data = csv .DictReader (f_csv ,dialect = dialect ,restkey = "UNKNOWN" ,restval = "" )
84+ fieldnames = data .fieldnames
85+ read_data = []
8386
84- return (fieldnames , read_data )
87+ for row in data :
88+ row ["dm_source_file" ] = os .path .split (path_to_csv )[1 ]
89+ read_data .append (row )
90+
91+ return (fieldnames , read_data )
92+ except Exception as e :
93+ print >> sys .stderr , "Failed to read file {0}: {1}" .format (os .path .split (path_to_csv )[1 ],e )
94+ return (False , False )
8595
8696
8797def write_csv (path_to_csv , header , data , ui = None , files = None ):
@@ -305,13 +315,21 @@ def mergeFolder(folder, destination, ui=None):
305315 elif filetype in [".xls" ,".xlsx" ]:
306316 (header , data ) = read_xls (os .path .join (folder ,datafile ))
307317
308- col_names = list (set (col_names ) | set (header ) )
309- total_data .extend (data )
310- counter += 1
311-
312- if not ui is None :
313- progress = int (counter / float (2 * len (valid_files )+ 1 )* 100 )
314- ui .progressBar .setValue (progress )
318+ # read_csv() can return (False, False) if csv file was invalid.
319+ # Therefore check if data is correct and only then add to total data.
320+ if header and data :
321+ # Make sure every column name only occurs once
322+ col_names = list (set (col_names ) | set (header ) )
323+
324+ # Add data to rest of data
325+ total_data .extend (data )
326+ counter += 1
327+
328+ if not ui is None :
329+ progress = int (counter / float (2 * len (valid_files )+ 1 )* 100 )
330+ ui .progressBar .setValue (progress )
331+ else :
332+ print >> sys .stderr , "Error reading {0}. Skipping ..." .format (os .path .split (datafile )[1 ])
315333
316334 print "Writing merged data to file (please be patient as this can take a while...)"
317335 #ui.progressBar.setValue(progress+1) #If this is ommitted, above line is not printed to textbox in GUI...
0 commit comments