From 22544777960f92573ff3e3232a316c76caefb9ec Mon Sep 17 00:00:00 2001 From: Tobias Schmidt <tobias.schmidt@unibas.ch> Date: Tue, 22 May 2012 12:55:09 +0200 Subject: [PATCH] TableClass: ost format loader supports quoted strings when loading the ost format, value strings were split even if they were quoted, which made it impossible to load certain files --- modules/base/pymod/table.py | 6 ++++-- modules/base/tests/test_table.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 284c0419f..0f902e3e1 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -426,8 +426,9 @@ class Table(object): self._AddRowsFromDict(data, overwrite) else: if len(data)!=len(self.col_names): + print data, self.col_names msg='data array must have %d elements, not %d' - raise ValueError(msg % (len(self.col_names), len(self.data))) + raise ValueError(msg % (len(self.col_names), len(data))) new_row = [self._Coerce(v, t) for v, t in zip(data, self.col_types)] # fully overwrite existing row with new data @@ -529,6 +530,7 @@ class Table(object): @staticmethod def _LoadOST(stream_or_filename): fieldname_pattern=re.compile(r'(?P<name>[^[]+)(\[(?P<type>\w+)\])?') + values_pattern=re.compile("([^\" ]+|\"[^\"]*\")+") if not hasattr(stream_or_filename, 'read'): stream=open(stream_or_filename, 'r') else: @@ -556,7 +558,7 @@ class Table(object): tab=Table(fieldnames, fieldtypes) header=True continue - tab.AddRow(line.split()) + tab.AddRow([x.strip('"') for x in values_pattern.findall(line)]) if num_lines==0: raise IOError("Cannot read table from empty stream") return tab diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index 3edf226be..ca6da2920 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -757,6 +757,18 @@ class TestTable(unittest.TestCase): in_stream = open(os.path.join('testfiles','emptytable.csv'), 'r') self.assertRaises(IOError, Table.Load, in_stream) + def testSaveLoadTableOSTWithSpaces(self): + tab = self.CreateTestTable() + tab.AddRow(['hello spaces',10, 10.1], overwrite=None) + self.CompareDataFromDict(tab, {'first': ['x','foo',None,'hello spaces'], 'second': [3,None,9,10], 'third': [None,2.2,3.3,10.1]}) + + # write to disc + tab.Save("saveloadtable_withspaces_filename_out.tab") + + # read from disc + tab_loaded_fname = Table.Load('saveloadtable_withspaces_filename_out.tab') + self.CompareDataFromDict(tab_loaded_fname, {'first': ['x','foo',None,'hello spaces'], 'second': [3,None,9,10], 'third': [None,2.2,3.3,10.1]}) + def testSaveLoadTableCSV(self): tab = self.CreateTestTable() self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) -- GitLab