python函数 - bytearray

Oct 17, 2016 pm 03:34 PM

bytearray([source [, encoding [, errors]]])

中文说明:

bytearray([source [, encoding [, errors]]])返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。


参数source:


如果source为整数,则返回一个长度为source的初始化数组;


如果source为字符串,则按照指定的encoding将字符串转换为字节序列;


如果source为可迭代类型,则元素必须为[0 ,255]中的整数;


如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。


版本:在python2.6后新引入,在python3中同样可以使用!


英文说明:

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0

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

Without an argument, an array of size 0 is created.

New in version 2.6.

实例演示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

>>> a = bytearray(3)

>>> a

bytearray(b'\x00\x00\x00')

>>> 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'\x01\x02\x03')

>>> c[0]

   

>>> c[1]

   

>>> c[2]

   

>>> d = bytearray(buffer("abc"))

>>> d

bytearray(b'abc')

>>> d[0]

   

>>> d[1]

   

>>> d[2]

Copier après la connexion

   


Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Comment utiliser Python pour trouver la distribution ZIPF d'un fichier texte Comment utiliser Python pour trouver la distribution ZIPF d'un fichier texte Mar 05, 2025 am 09:58 AM

Comment utiliser Python pour trouver la distribution ZIPF d'un fichier texte

Comment utiliser la belle soupe pour analyser HTML? Comment utiliser la belle soupe pour analyser HTML? Mar 10, 2025 pm 06:54 PM

Comment utiliser la belle soupe pour analyser HTML?

Filtrage d'image en python Filtrage d'image en python Mar 03, 2025 am 09:44 AM

Filtrage d'image en python

Comment effectuer l'apprentissage en profondeur avec TensorFlow ou Pytorch? Comment effectuer l'apprentissage en profondeur avec TensorFlow ou Pytorch? Mar 10, 2025 pm 06:52 PM

Comment effectuer l'apprentissage en profondeur avec TensorFlow ou Pytorch?

Introduction à la programmation parallèle et simultanée dans Python Introduction à la programmation parallèle et simultanée dans Python Mar 03, 2025 am 10:32 AM

Introduction à la programmation parallèle et simultanée dans Python

Sérialisation et désérialisation des objets Python: partie 1 Sérialisation et désérialisation des objets Python: partie 1 Mar 08, 2025 am 09:39 AM

Sérialisation et désérialisation des objets Python: partie 1

Comment implémenter votre propre structure de données dans Python Comment implémenter votre propre structure de données dans Python Mar 03, 2025 am 09:28 AM

Comment implémenter votre propre structure de données dans Python

Modules mathématiques en python: statistiques Modules mathématiques en python: statistiques Mar 09, 2025 am 11:40 AM

Modules mathématiques en python: statistiques

See all articles