bytearray([source [, encoding [, errors]]])
中文說(shuō)明:
bytearray([source [, encoding [, errors]]])返回一個(gè)byte數(shù)組。Bytearray類(lèi)型是一個(gè)可變的序列,并且序列中的元素的取值范圍為 [0 ,255]。
參數(shù)source:
如果source為整數(shù),則返回一個(gè)長(zhǎng)度為source的初始化數(shù)組;
如果source為字符串,則按照指定的encoding將字符串轉(zhuǎn)換為字節(jié)序列;
如果source為可迭代類(lèi)型,則元素必須為[0 ,255]中的整數(shù);
如果source為與buffer接口一致的對(duì)象,則此對(duì)象也可以被用于初始化bytearray.。
版本:在python2.6后新引入,在python3中同樣可以使用!
英文說(shuō)明:
Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.
The optional source parameter can be used to initialize the array in a few different ways:
If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
New in version 2.6.
實(shí)例演示:
>>> a = bytearray(3) >>> a bytearray(b'x00x00x00') >>> a[0] >>> a[1] >>> a[2] >>> b = bytearray("abc") >>> b bytearray(b'abc') >>> b[0] >>> b[1] >>> b[2] >>> c = bytearray([1, 2, 3]) >>> c bytearray(b'x01x02x03') >>> c[0] >>> c[1] >>> c[2] >>> d = bytearray(buffer("abc")) >>> d bytearray(b'abc') >>> d[0] >>> d[1] >>> d[2]
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com