This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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