如何在Python 3中在字节和字符串之间转换? [英] How to convert between bytes and strings in Python 3?
问题描述
这是一个Python 101类型的问题,但是当我尝试使用一个似乎将我的字符串输入转换为字节的包时,让我感到困惑。
This is a Python 101 type question, but it had me baffled for a while when I tried to use a package that seemed to convert my string input into bytes.
您会在下面看到我自己找到的答案,但是我觉得这里值得记录,因为我花了很多时间来发掘正在发生的事情。它似乎是Python 3的通用名称,因此我没有提到我正在使用的原始程序包。这似乎不是一个错误(只是特定的程序包具有 .tostring()
方法,显然不是会产生我理解的方法一个字符串...)
As you will see below I found the answer for myself, but I felt it was worth recording here because of the time it took me to unearth what was going on. It seems to be generic to Python 3, so I have not referred to the original package I was playing with; it does not seem to be an error (just that the particular package had a .tostring()
method that was clearly not producing what I understood as a string...)
我的测试程序如下:
import mangler # spoof package
stringThing = """
<Doc>
<Greeting>Hello World</Greeting>
<Greeting>你好</Greeting>
</Doc>
"""
# print out the input
print('This is the string input:')
print(stringThing)
# now make the string into bytes
bytesThing = mangler.tostring(stringThing) # pseudo-code again
# now print it out
print('\nThis is the bytes output:')
print(bytesThing)
此代码的输出给出以下内容:
The output from this code gives this:
This is the string input:
<Doc>
<Greeting>Hello World</Greeting>
<Greeting>你好</Greeting>
</Doc>
This is the bytes output:
b'\n<Doc>\n <Greeting>Hello World</Greeting>\n <Greeting>\xe4\xbd\xa0\xe5\xa5\xbd</Greeting>\n</Doc>\n'
因此,需要能够在字节和字符串之间进行转换,以避免最终将非ascii字符转换为gobbledegook。
So, there is a need to be able to convert between bytes and strings, to avoid ending up with non-ascii characters being turned into gobbledegook.
推荐答案
上述代码示例中的 mangler正在执行以下操作:
The 'mangler' in the above code sample was doing the equivalent of this:
bytesThing = stringThing.encode(encoding='UTF-8')
还有其他方式可以编写此代码(特别是使用 bytes(stringThing,encoding ='UTF-8')
,但是上面的语法可以很清楚地知道发生了什么,并且恢复字符串的操作:
There are other ways to write this (notably using bytes(stringThing, encoding='UTF-8')
, but the above syntax makes it obvious what is going on, and also what to do to recover the string:
newStringThing = bytesThing.decode(encoding='UTF-8')
这样做时,将恢复原始字符串。
When we do this, the original string is recovered.
注意,使用 str(bytesThing)
只会转录所有gobbledegook,而无需将其转换回Unicode,除非您特别要求使用UTF-8,即 str( bytesThing,encoding ='UTF-8')
。如果未指定编码,则不会报告错误。
Note, using str(bytesThing)
just transcribes all the gobbledegook without converting it back into Unicode, unless you specifically request UTF-8, viz., str(bytesThing, encoding='UTF-8')
. No error is reported if the encoding is not specified.
这篇关于如何在Python 3中在字节和字符串之间转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!