From 54c6622ad9c4ed9d8b395fc57907c951bacce20f Mon Sep 17 00:00:00 2001 From: Georg Osang Date: Wed, 8 Feb 2023 10:47:26 +0100 Subject: [PATCH] CellParser: Trim starting/trailing whitespace --- parsers/common/cellparser.py | 3 ++- parsers/common/tests/test_cellparser.py | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/parsers/common/cellparser.py b/parsers/common/cellparser.py index 1a87e5c..0d01bb7 100644 --- a/parsers/common/cellparser.py +++ b/parsers/common/cellparser.py @@ -28,7 +28,8 @@ def split_into_lists(self, string): def cleanse(self, nested_list): # Unescape escaped characters (\, |, ;) if type(nested_list) == str: - return nested_list.replace('\\\\', '\1') \ + return nested_list.strip() \ + .replace('\\\\', '\1') \ .replace('\\|', '|') \ .replace('\\;', ';') \ .replace('\1', '\\') diff --git a/parsers/common/tests/test_cellparser.py b/parsers/common/tests/test_cellparser.py index 0fd950e..1821a12 100644 --- a/parsers/common/tests/test_cellparser.py +++ b/parsers/common/tests/test_cellparser.py @@ -44,7 +44,7 @@ def test_cleanse(self): self.compare_cleanse('a\\\\', 'a\\') self.compare_cleanse('a\\\\;', 'a\\;') self.compare_cleanse([[['a\\;']]], [[['a;']]]) - self.compare_cleanse(['\\\\', '\n\\;\n'], ['\\', '\n;\n']) + self.compare_cleanse(['\\\\', '\\;'], ['\\', ';']) def test_split_into_lists(self): self.compare_split_into_lists('1', '1') @@ -69,6 +69,12 @@ def test_split_into_lists(self): self.compare_split_into_lists('1;2|3;4\\|', [['1', '2'], ['3', '4|']]) self.compare_split_into_lists(CellParser.escape_string('|;;|\\;\\\\\\|'), '|;;|\\;\\\\\\|') + def test_string_stripping(self): + self.compare_cleanse(' a;\n', 'a;') + self.compare_cleanse([' a;\n'], ['a;']) + self.compare_split_into_lists(' a\n|\nb ', ['a', 'b']) + self.compare_split_into_lists('1; 2\n|\n3; 4', [['1', '2'], ['3', '4']]) + class TestCellParser(unittest.TestCase):