Skip to content

Instantly share code, notes, and snippets.

@dushujun
Created October 18, 2016 04:07
Show Gist options
  • Save dushujun/5e93bc5fbf346705718683d47a8bd113 to your computer and use it in GitHub Desktop.
Save dushujun/5e93bc5fbf346705718683d47a8bd113 to your computer and use it in GitHub Desktop.
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))
else:
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