bytes

class bytes(object):
    """
    bytes(iterable_of_ints) -> bytes
    bytes(string, encoding[, errors]) -> bytes
    bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
    bytes(int) -> bytes object of size given by the parameter initialized with null bytes
    bytes() -> empty bytes object

    Construct an immutable array of bytes from:
      - an iterable yielding integers in range(256)
      - a text string encoded using the specified encoding
      - any object implementing the buffer API.
      - an integer
    """

Python中的字节码用b‘xxx‘的形式表示。x可以用字符表示,也可以用ASCII编码形式\xnn表示,nn从00-ff(十六进制)共256种字符。 

a = ‘abc‘
b = bytes(a, ‘utf-8‘)
print(b)
c = bytes(a, ‘gbk‘)
print(c)
# 结果 b‘abc‘
# 结果 b‘abc‘

a = ‘你好‘
b = bytes(a, ‘utf-8‘)
print(b)
c = bytes(a, ‘gbk‘)
print(c)
# 结果 b‘\xe4\xbd\xa0\xe5\xa5\xbd‘
# 结果 b‘\xc4\xe3\xba\xc3‘

decode

c = bytes(a, ‘gbk‘).decode(‘gbk‘)
print(c)
# 结果 你好

  

时间: 2024-08-29 18:10:20

bytes的相关文章

Python内置函数——bytes

英文文档: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the

bytes数据类型,三元运算,进制互换

三元运算 如果这个条件成立就存这个值,如果那个条件成立就存那个值. 进制 bytes类型,字节数据类型也就是二进制类型,这个是python3专有数据类型,在python2里跟字符串是一个类型,也就是python2是不区分这个数据类型的. 比如说音频,视频文件都是二进制类型,也就是bytes类型.(python3通过socket在网络上传输数据时必须要用二进制格式,python2没有强制必须是二进制,字符串也可以) Python3中最大的新特性就是对文本和二进制数据做了更清晰的区分.文本通常是Un

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us     ers_email_unique`(`email`)) public function up() { Schema::create('users', function (Blu

nginx报错 client intended to send too large body: 1331696 bytes

1,nginx后台error日志报错 2016/02/05 16:23:56 [error] 12024#0: *441106971 connect() failed (111: Connection refused) while connecting to upstream, client: 113.214.1.10, server: localhost, request: "GET /h5teb/ugcH5/index.htm?source=android&mall=8&TG

byte[] bytes和string转换

public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "        {            string hexString = string.Empty;            if ( bytes != null )            {                StringBuilder strB = new StringBuilder ();            

Python 关于bytes类方法对数字转换的误区, Json的重要性

本文起源于一次犯错, 在发觉bytes()里面可以填数字, 转出来的也是bytes类型, 就心急把里面的东西decode出来. 结果为空.搞来搞去以为是命令不熟练事实上错在逻辑. a1 = bytes('11', encoding='utf-8') print(a1) b1 = a1.decode() print(b1) a2 = bytes(11) print(a2) b2 = a2.decode() print(b2) 错误的逻辑: bytes 把 int 转成 byte类型,  然后把by

PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744疑问:786432 是指786432bytes?即:768MB 98304 bytes=9

MySQL测试环境遇到 mmap(xxx bytes) failed; errno 12解决方法

查看Mysql日志 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap(137363456 bytes) failed; errno 12 InnoDB: Completed initialization of buffer pool InnoDB: Fatal error: cannot allocate memory for the buffer pool [ERROR] Plugin 'InnoDB' init func

Python3 关于UnicodeDecodeError/UnicodeEncodeError: ‘gbk’ codec can’t decode/encode bytes类似的文本编码问题

以下是小白的爬虫学习历程中遇到并解决的一些困难,希望写出来给后来人,如有疏漏恳请大牛指正,不胜感谢! 首先,我的代码是这样的 1 2 3 import requests 4 5 url = 'http://www.acfun.tv/' 6 html = requests.get(url) 7 8 print(html.text) python2中解决方法(题外话) 参考:http://www.cnblogs.com/zhaoyl/p/3770340.html 在前面加上以下代码即可 import

Golang之bytes.buffer

bytes.buffer是一个缓冲byte类型的缓冲器存放着都是byte Buffer 是 bytes 包中的一个 type Buffer struct{-} A buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use. (是一个变长的 buffer,具有 Read 和Write 方法. Buf