如何将 Python 3 字节字符串变量转换为常规字符串? [英] How do I convert a Python 3 byte-string variable into a regular string?
问题描述
我已经阅读了一个 XML 电子邮件附件
bytes_string=part.get_payload(decode=False)
正如我的变量名称所暗示的那样,有效载荷以字节字符串的形式出现.
我正在尝试使用推荐的 Python 3 方法将此字符串转换为我可以操作的可用字符串.
示例显示:
<块引用>str(b'abc','utf-8')
如何将 b
(字节)关键字参数应用于我的变量 bytes_string
并使用推荐的方法?
我尝试的方法不起作用:
str(bbytes_string, 'utf-8')
你在最后一行几乎正确.你想要
str(bytes_string, 'utf-8')
因为bytes_string
的类型是bytes
,与b'abc'
的类型相同.
I have read in an XML email attachment with
bytes_string=part.get_payload(decode=False)
The payload comes in as a byte string, as my variable name suggests.
I am trying to use the recommended Python 3 approach to turn this string into a usable string that I can manipulate.
The example shows:
str(b'abc','utf-8')
How can I apply the b
(bytes) keyword argument to my variable bytes_string
and use the recommended approach?
The way I tried doesn't work:
str(bbytes_string, 'utf-8')
You had it nearly right in the last line. You want
str(bytes_string, 'utf-8')
because the type of bytes_string
is bytes
, the same as the type of b'abc'
.
这篇关于如何将 Python 3 字节字符串变量转换为常规字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!