Nếu tôi có danh sách Python [
newba = bytearray[]
4] và hai số nguyên [newba = bytearray[]
5 và newba = bytearray[]
6], tôi có thể dễ dàng nối các bản sao newba = bytearray[]
6 của newba = bytearray[]
5 lên newba = bytearray[]
4:>>> data = [0]
>>> i, count = 1, 4
>>> data += [i] * count
>>> data
[0, 1, 1, 1, 1]
Làm thế nào tôi có thể làm điều tương tự nếu
newba = bytearray[]
4 là for baelement in fileba
newba += b'\xC0' #This works fine!
newba += fileba [0] #Doesn't work - "can't concat int to bytearray"
newba += baelement #Doesn't work - "can't concat int to bytearray"
newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
1? Có cần phải tạo một danh sách trước, ví dụ: for baelement in fileba
newba += b'\xC0' #This works fine!
newba += fileba [0] #Doesn't work - "can't concat int to bytearray"
newba += baelement #Doesn't work - "can't concat int to bytearray"
newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
2?
Hỏi ngày 10 tháng 4 năm 2016 lúc 1:40Apr 10, 2016 at 1:40
user200783user200783user200783
13.3k11 Huy hiệu vàng63 Huy hiệu bạc125 Huy hiệu Đồng11 gold badges63 silver badges125 bronze badges
1
Nếu bạn không ngại sử dụng
for baelement in fileba
newba += b'\xC0' #This works fine!
newba += fileba [0] #Doesn't work - "can't concat int to bytearray"
newba += baelement #Doesn't work - "can't concat int to bytearray"
newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
3, tôi sẽ đề xuất for baelement in fileba
newba += b'\xC0' #This works fine!
newba += fileba [0] #Doesn't work - "can't concat int to bytearray"
newba += baelement #Doesn't work - "can't concat int to bytearray"
newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
4.>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
Đã trả lời ngày 10 tháng 4 năm 2016 lúc 2:12Apr 10, 2016 at 2:12
4
Có cần phải tạo một danh sách trước, ví dụ:
2?for baelement in fileba newba += b'\xC0' #This works fine! newba += fileba [0] #Doesn't work - "can't concat int to bytearray" newba += baelement #Doesn't work - "can't concat int to bytearray" newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
Đó thực sự là một cách để làm điều đó. Bạn có thể thực hiện thao tác nhân trên mảng byte [trái ngược với danh sách], hiệu quả hơn một chút về bộ nhớ và nhanh hơn nhiều cho các giá trị lớn là ____ 46*:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
* Nguồn: Hoạt động trên máy của tôi; Ymmv! Đã thử với số lượng 4000000000, đã ra khỏi bộ nhớ khi nhân danh sách, nhưng không phải khi nhân bytearray. Các giá trị nhỏ hơn của
newba = bytearray[]
6 [600000000] sử dụng bộ nhớ nhiều hơn 8 lần khi nhân danh sách so với khi nhân một bytearray.Đã trả lời ngày 10 tháng 4 năm 2016 lúc 2:04Apr 10, 2016 at 2:04
Score_UnderScore_UnderScore_Under
1.14910 Huy hiệu bạc18 Huy hiệu đồng10 silver badges18 bronze badges
Tôi đang cố gắng thêm các yếu tố từ bytearray này sang bytearray khác. Đây là nguồn bytearray nguồn:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
Đây là bytearray mới được xây dựng từ đầu:
newba = bytearray[]
Vấn đề là khi tôi cố gắng sao chép các phần tử byte từ cái cũ sang cái mới:
for baelement in fileba
newba += b'\xC0' #This works fine!
newba += fileba [0] #Doesn't work - "can't concat int to bytearray"
newba += baelement #Doesn't work - "can't concat int to bytearray"
newba.extend [baelement ] #Doesn't work - "'int' object is not iterable"
Tôi đang sử dụng Python 2.7.6, nhưng không thể sao chép byte từ bytearray này sang bên kia .. Điều gì có thể là vấn đề?
Byte, bytearray
Python hỗ trợ một loạt các loại để lưu trữ trình tự. Có sáu loại chuỗi: chuỗi, chuỗi byte [các đối tượng byte], mảng byte [đối tượng bytearray], danh sách, bộ dữ liệu và các đối tượng phạm vi.
Chuỗi chứa các ký tự unicode. Các chữ của chúng được viết bằng trích dẫn đơn hoặc đôi: 'Python', "Dữ liệu". Các đối tượng byte và bytearray chứa các byte đơn - cái trước là bất biến trong khi cái sau là một chuỗi có thể thay đổi. Các đối tượng byte có thể được xây dựng hàm tạo, byte [] và từ các chữ; Sử dụng tiền tố B với cú pháp chuỗi bình thường: b'python '. Để xây dựng các mảng byte, hãy sử dụng hàm bytearray [].
BYTE LIÊN QUAN
bytesliteral ::= bytesprefix[shortbytes | longbytes] bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= longbyteschar ::= bytesescapeseq ::= "\"
Các hàm byte [] và bytearray []
chức năng byte []:
Trả về một đối tượng "byte" mới, là một chuỗi các số nguyên nhỏ bất biến trong phạm vi 0
Syntax:
bytes[[source[, encoding[, errors]]]]
chức năng bytearray []::
Trả về một mảng byte mới. Loại bytearray là một chuỗi các số nguyên có thể thay đổi trong phạm vi 0
Syntax:
bytearray[[source[, encoding[, errors]]]]
Tham số nguồn tùy chọn có thể được sử dụng để khởi tạo mảng theo một vài cách khác nhau:
- Nếu đó là một chuỗi, bạn cũng phải cung cấp các tham số mã hóa [và tùy chọn, lỗi]; bytearray [] sau đó chuyển đổi chuỗi thành byte bằng str.encode [].
- Nếu đó là một số nguyên, mảng sẽ có kích thước đó và sẽ được khởi tạo bằng các byte null.
- Nếu nó là một đối tượng phù hợp với giao diện bộ đệm, bộ đệm chỉ đọc của đối tượng sẽ được sử dụng để khởi tạo mảng byte.
- Nếu nó có thể hiểu được, nó phải là một số nguyên của các số nguyên trong phạm vi 0
Không có đối số, một mảng có kích thước 0 được tạo.
Tạo đối tượng byte trong Python
Ví dụ 1 :
Mã số :
>>> x = b"Bytes objects are immutable sequences of single bytes"
>>> print[x]
b'Bytes objects are immutable sequences of single bytes'
>>>
Ví dụ-2:
Code:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
0Output:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
1Ví dụ-3:
Mã số :
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
2Output:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
3Ví dụ-2:
Ví dụ-3:
Code:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
4Output:
Chuyển đổi byte thành chuỗiVí dụ-2:
Code:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
6Output:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
7Ví dụ-3:
Code:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
8Output:
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
9Chuyển đổi byte thành chuỗi
Ví dụ-3:
Mã số :
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
0Output:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
1Ví dụ-2:
Mã số :
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
2Output:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
3Ví dụ-2:
Ví dụ-3:
Code:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
4Output:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
5Ví dụ-2:
Code:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
6Output:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
7Ví dụ-3:
Ví dụ-3:
Code:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
8Output:
>>> data = bytearray[[0]]
>>> i, count = 1, 4
>>> data += bytearray[[i,]] * count
>>> data
bytearray[b'\x00\x01\x01\x01\x01']
9Ví dụ-2:
Code:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
0Output:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
1Example-3:
Code:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
2Output:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
3Ví dụ-3:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
4Chuyển đổi byte thành chuỗi
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
5Ví dụ 1:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
6>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
5
>>> from itertools import repeat
>>> data = bytearray[[0]]
>>> data.extend[repeat[1, 3]]
>>> data
bytearray[b'\x00\x01\x01\x01']
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
7Ví dụ-3:
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
8Chuyển đổi chuỗi hex thành byte
filedata = open["file.bin", "rb"].read[]
fileba = bytearray[filedata]
9Mã số đại diện cho một ký tự của đối tượng byte trong Python
Xác định một ký tự bảng ánh xạ để sử dụng với đối tượng byte trong PythonChuyển đổi byte thành hex trong python
newba = bytearray[]
1Cách lấy ký tự từ mã số trong các đối tượng byte trong Python
newba = bytearray[]
2Xác định độ dài của một đối tượng byte trong Python
newba = bytearray[]
3Sử dụng các toán tử + và * với các đối tượng byte trong Python Python break, continue
Next: Python String
Làm thế nào để có được một byte từ một đối tượng byte trong Python?