Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何转换<;类别str>&引用;二进制“U字符串”;至<;类字节>;b";二进制字符串;? 问题:_Python 3.x - Fatal编程技术网

Python 3.x 如何转换<;类别str>&引用;二进制“U字符串”;至<;类字节>;b";二进制字符串;? 问题:

Python 3.x 如何转换<;类别str>&引用;二进制“U字符串”;至<;类字节>;b";二进制字符串;? 问题:,python-3.x,Python 3.x,如何从源转换为输出 这两个字符串之间的关系 这里的问题到底是什么?如何将unicode str转换为bytes str,但unicode str中的内容实际上是bytes str的内容。 >>> source = '\xe6\x88\x91\xe5\x80\x91' >>> output = bytes('我們', 'utf8') >>> type(source) <class 'str'> >>> type(

如何从
转换为
输出

这两个字符串之间的关系
这里的问题到底是什么?如何将unicode str转换为bytes str,但unicode str中的内容实际上是bytes str的内容。
>>> source = '\xe6\x88\x91\xe5\x80\x91'
>>> output = bytes('我們', 'utf8')
>>> type(source)
<class 'str'>
>>> type(output)
<class 'bytes'>
>>> output
b'\xe6\x88\x91\xe5\x80\x91'
>>> convert_func = lambda s: s.encode('utf8')
>>>
>>> output = bytes('abcde', 'utf8')
>>> output
b'abcde'
>>> source = 'abcde'
>>> convert_func(source) == output
True
>>>
>>> output = bytes('我們', 'utf8')
>>> source = '\xe6\x88\x91\xe5\x80\x91'
>>> convert_func(source) == output
False