python3 str或bytes转换函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 str或bytes转换函数相关的知识,希望对你有一定的参考价值。

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转换函数的主要内容,如果未能解决你的问题,请参考以下文章