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
本文提供了两个Python函数,用于在字符串(str)和字节(bytes)类型间进行转换。第一个函数将bytes转换为str,第二个函数则将str编码为bytes。这些函数在处理文本和二进制数据时非常有用。

被折叠的 条评论
为什么被折叠?



