码迷,mamicode.com
首页 > 编程语言 > 详细

python3 str或bytes转换函数

时间:2017-09-07 19:30:02      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:python3   usr   utf-8   class   pytho   bin   value   byte   pre   

str或bytes始终返回为str

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def to_str(bytes_or_str):
    if isinstance(bytes_or_str, bytes):
        value = bytes_or_str.decode(utf-8)
    else:
        value = bytes_or_str
    return value    #Instance of str

str或bytes始终返回为bytes

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def to_str(bytes_or_str):
    if isinstance(bytes_or_str, str):
        value = bytes_or_str.encode(utf-8)
    else:
        value = bytes_or_str
    return value    #Instance of bytes

 

 

 

  

python3 str或bytes转换函数

标签:python3   usr   utf-8   class   pytho   bin   value   byte   pre   

原文地址:http://www.cnblogs.com/chengd/p/7491041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!