def to_utf8(s):
"""Convert a string to utf8. If the argument is an iterable
(list/tuple/set), then each element of it would be converted instead.
>>> to_utf8('a')
'a'
>>> to_utf8(u'a')
'a'
>>> to_utf8([u'a', u'b', u'\u4f60'])
['a', 'b', '\\xe4\\xbd\\xa0']
"""
if six.PY2:
if isinstance(s, str):
return s
elif isinstance(s, unicode):
return s.encode('utf-8')
elif isinstance(s, (list, tuple, set)):
return [to_utf8(v) for v in s]
else:
return s
else:
return s
评论列表
文章目录