From bb78725a05a69f41fc5775efaa0a0fe80fa01815 Mon Sep 17 00:00:00 2001 From: nicofrand Date: Mon, 22 Jan 2018 12:50:37 +0100 Subject: [PATCH] Trim whitespaces in string normalization --- flatisfy/tests.py | 9 +++++++++ flatisfy/tools.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/flatisfy/tests.py b/flatisfy/tests.py index 1b7e34c..b74e3d9 100644 --- a/flatisfy/tests.py +++ b/flatisfy/tests.py @@ -114,6 +114,15 @@ class TestTexts(unittest.TestCase): tools.normalize_string("avec ascenseur") ) + def test_whitespace_trim(self): + """ + Checks that trailing and beginning whitespaces are trimmed. + """ + self.assertEqual( + "rennes 35000", + tools.normalize_string(" Rennes 35000 ") + ) + def test_accents(self): """ Checks accents are replaced. diff --git a/flatisfy/tools.py b/flatisfy/tools.py index 59cc8eb..3c500cb 100644 --- a/flatisfy/tools.py +++ b/flatisfy/tools.py @@ -225,6 +225,9 @@ def normalize_string(string, lowercase=True, convert_arabic_numerals=True): # Collapse multiple spaces, replace tabulations and newlines by space string = re.sub(r"\s+", " ", string) + # Trim whitespaces + string = string.strip() + return string