在Python 2.7中将字符串更改为字节类型 [英] Changing string to byte type in Python 2.7
问题描述
在python 3.2中,我可以轻松更改对象的类型.例如:
In python 3.2, i can change the type of an object easily. For example :
x=0
print(type (x))
x=bytes(0)
print(type (x))
它会给我这个:
<class 'int'>
<class 'bytes'>
但是,在python 2.7中,似乎我无法使用相同的方法来执行此操作.如果我执行相同的代码,它将给我:
But, in python 2.7, it seems that i can't use the same way to do it. If i do the same code, it give me this :
<type 'int'>
<type 'str'>
如何将类型更改为字节类型?
What can i do to change the type into a bytes type?
推荐答案
如何将类型更改为字节类型?
您不能,在Python 2.7中没有像字节"这样的类型.
You can't, there is no such type as 'bytes' in Python 2.7.
摘自Python 2.7文档(5.6序列类型): 有七种序列类型:字符串,Unicode字符串,列表,元组,字节数组,缓冲区和xrange对象."
From the Python 2.7 documentation (5.6 Sequence Types): "There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects."
来自Python 3.2文档(5.6序列类型): 有六种序列类型:字符串,字节序列(字节对象),字节数组(字节数组对象),列表,元组和范围对象."
From the Python 3.2 documentation (5.6 Sequence Types): "There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects."
这篇关于在Python 2.7中将字符串更改为字节类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!