首页 > 其他 > 详细

转换为系统支持的字符串

时间:2018-08-10 10:38:10      阅读:238      评论:0      收藏:0      [点我收藏+]

import sys

builtin_str = str
ver = sys.version
is_py2 = int(ver[0]) == 2
is_py3 = int(ver[0]) == 3


def to_native_string(string, encoding=‘ascii‘):
"""Given a string object, regardless of type, returns a representation of
that string in the native string type, encoding and decoding where
necessary. This assumes ASCII unless told otherwise.
"""
if isinstance(string, builtin_str):
out = string
else:
if is_py2:
out = string.encode(encoding)
# out = string.decode(encoding)
else:

out = string.decode(encoding)

return out


print to_native_string("just a test",‘utf-8‘)

转换为系统支持的字符串

原文:https://www.cnblogs.com/tnyleyon/p/9452851.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!