Repository: INM-6/python-odmltables

Issue:
Error when converting empty (apart from header) csv file
'row_id' is undefined here when the csv file only contains a header:

https://github.com/INM-6/python-odmltables/blob/f9787990e4865f222fb072e8c5c84f96a2bada6c/odmltables/odml_table.py#L492

Comments:

Patch:
diff --git a/odmltables/odml_table.py b/odmltables/odml_table.py
index 7ac58aa..f6bf460 100644
--- a/odmltables/odml_table.py
+++ b/odmltables/odml_table.py
@@ -399,6 +399,8 @@ class OdmlTable(object):
         # create a inverted header_titles dictionary for an inverted lookup
         inv_header_titles = {v: k for (k, v) in list(self._header_titles.items())}
 
+        row_id = None
+
         with open(load_from, 'r') as csvfile:
             csvreader = csv.reader(csvfile)
 
@@ -489,10 +491,11 @@ class OdmlTable(object):
                     current_dic = new_dic
 
             # copy final property
-            if row_id == 0:
-                self._odmldict.append(copy.deepcopy(new_dic))
-            else:
-                self._odmldict.append(copy.deepcopy(current_dic))
+            if not (row_id is None):
+                if row_id == 0:
+                    self._odmldict.append(copy.deepcopy(new_dic))
+                else:
+                    self._odmldict.append(copy.deepcopy(current_dic))
 
             # value conversion for all properties
             for current_dic in self._odmldict:


Test patch:
diff --git a/odmltables/tests/test_odml_csv_table.py b/odmltables/tests/test_odml_csv_table.py
index aee45c0..9d07022 100644
--- a/odmltables/tests/test_odml_csv_table.py
+++ b/odmltables/tests/test_odml_csv_table.py
@@ -76,6 +76,22 @@ class TestOdmlCsvTable(unittest.TestCase):
         self.assertEqual(len(table._odmldict), 1)
         self.assertDictEqual(table._odmldict[0], table2._odmldict[0])
 
+    def test_saveload_empty_header(self):
+        doc = odml.Document()
+
+        table = OdmlCsvTable()
+        table.load_from_odmldoc(doc)
+        table.change_header('full')
+        table.write2file(self.filename)
+
+        table2 = OdmlTable()
+        table2.load_from_csv_table(self.filename)
+
+        # comparing values which are written to xls by default
+        self.assertEqual(table._odmldict, [])
+        self.assertDictEqual({'author': None, 'date': None, 'repository': None, 'version': None},
+                             table._docdict)
+
 
 class TestShowallOdmlCsvTable(unittest.TestCase):
     """
diff --git a/odmltables/tests/test_odml_xls_table.py b/odmltables/tests/test_odml_xls_table.py
index 2998882..b386a1d 100644
--- a/odmltables/tests/test_odml_xls_table.py
+++ b/odmltables/tests/test_odml_xls_table.py
@@ -232,6 +232,22 @@ class TestOdmlXlsTable(unittest.TestCase):
         self.assertEqual(len(table._odmldict), 1)
         self.assertDictEqual(table._odmldict[0], table2._odmldict[0])
 
+    def test_saveload_empty_header(self):
+        doc = odml.Document()
+
+        table = OdmlXlsTable()
+        table.load_from_odmldoc(doc)
+        table.change_header('full')
+        table.write2file(self.filename)
+
+        table2 = OdmlTable()
+        table2.load_from_xls_table(self.filename)
+
+        # comparing values which are written to xls by default
+        self.assertEqual(table._odmldict, [])
+        self.assertDictEqual({'author': None, 'date': None, 'repository': None, 'version': None},
+                             table._docdict)
+
 
 class TestShowallOdmlXlsTable(unittest.TestCase):
     """