diff --git a/benedict/utils/parse_util.py b/benedict/utils/parse_util.py index 4977b8b..68e2db0 100644 --- a/benedict/utils/parse_util.py +++ b/benedict/utils/parse_util.py @@ -5,6 +5,7 @@ from dateutil import parser as date_parser from decimal import Decimal, DecimalException from MailChecker import MailChecker from phonenumbers import phonenumberutil, PhoneNumberFormat +from six import string_types from slugify import slugify import ftfy @@ -164,10 +165,14 @@ def parse_slug(val): def parse_str(val): - val = str(val).strip() - try: - val = ftfy.fix_text(val) - except UnicodeError: - pass + if (isinstance(val, string_types)): + try: + val = ftfy.fix_text(val) + except UnicodeError: + pass + else: + val = str(val) + val = val.strip() + val = ' '.join(val.split()) return val diff --git a/tests/test_parse_dict.py b/tests/test_parse_dict.py index 855af16..21561dd 100644 --- a/tests/test_parse_dict.py +++ b/tests/test_parse_dict.py @@ -433,19 +433,23 @@ class ParseDictTestCase(unittest.TestCase): def test_get_str(self): d = { 'a': 'Hello World', - 'b': 1, + 'b': 'Hello World', + 'c': 1, } b = ParseDict(d) self.assertEqual(b.get_str('a'), 'Hello World') - self.assertEqual(b.get_str('b'), '1') + self.assertEqual(b.get_str('b'), 'Hello World') + self.assertEqual(b.get_str('c'), '1') # # only python 3 # def test_get_str_fix_encoding(self): # d = { # 'a': 'Sexâ\x80\x99n Drug', + # 'b': 'Localit\xe0', # } # b = ParseDict(d) - # self.assertEqual(b.get_str('a'), 'Sex\'n Drug') + # # self.assertEqual(b.get_str('a'), 'Sex\'n Drug') + # # self.assertEqual(b.get_str('b'), 'Località') def test_get_str_list(self): d = {