Fixed parse_str UnicodeEncodeError on python 2.
This commit is contained in:
parent
158f7180d3
commit
ece93946b7
|
@ -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
|
||||
|
||||
|
|
|
@ -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 = {
|
||||
|
|
Loading…
Reference in New Issue