Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 904ad47

Browse files
committed
Bump to v1.0.3
Ready for release - Progress indicator now works reliably - No longer a out of memory error when writing large xlsx files - Prevented crash when encountering invalid csv file. The file is now simply skipped and continued with the rest.
1 parent 45b2cbe commit 904ad47

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Datamerger
22
==========
3-
Copyright Daniel Schreij (2013)
3+
Copyright Daniel Schreij (2013-2014)
44

55
ABOUT
66
-----
7-
Current version: 1.02
7+
Current version: 1.0.3
88

99
Datamerger can merge separate spreadsheets into one large spreadsheet. While doing so,
1010
it takes column names into account, and therefore can correct for small inconsistenties

libdatamerger/datamerger_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def selectInputFolder(self):
142142
self.ui.progressBar.setValue(0)
143143

144144
def selectOutputDestination(self):
145-
selectedDest = QtGui.QFileDialog.getSaveFileName(self,"Save output as..",self.ui.outputFileDestination.text(),".csv .xls .xlsx")
145+
selectedDest = QtGui.QFileDialog.getSaveFileName(self,"Save output as..",self.ui.outputFileDestination.text(),"*.csv; *.xls; *.xlsx")
146146
# Prevent erasing previous entry on cancel press
147147
if selectedDest:
148148
self.destinationFile = selectedDest

libdatamerger/sheet_io_tools.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

8797
def 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

Comments
 (0)