Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2016 04:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
def str2unicode(data):
"""从bt拿到的任务参数是str, 需要转换成unicode"""
if isinstance(data, str):
return data.decode('utf8')
elif isinstance(data, unicode):
return data
elif isinstance(data, collections.Mapping):
return dict(map(str2unicode, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(str2unicode, data))
return data
def test_str2unicode():
# from yorg.contrib.cninfo import str2unicode
assert str2unicode('test') == 'test'
assert str2unicode('测试') == '测试'
assert str2unicode('测试'.encode('utf8')) == '测试'
assert str2unicode({'测试': 'test'})['测试'] == 'test'
assert str2unicode(['测试', 'test']) == ['测试', 'test']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment