Hàm thuận tiện để ghi mảng int vào tệp,
def write_array[fname,ray]:
'''
fname is a file pathname
ray is an array of int
'''
print["write:",fname]
EncodeInit[]
buffer = [ encode[z] for z in ray ]
some = bytearray[buffer]
immutable = bytes[some]
with open[fname,"wb"] as bfh:
wc = bfh.write[immutable]
print["wrote:",wrote]
return wc
Cách gọi chức năng,
write_array["data/filename",[1,2,3,4,5,6,7,8]]
Và bọc các phần sau trong một lớp để mã hóa/giải mã có thể đọc được:
Encode = {}
Decode = {}
def EncodeInit[]:
'''
Encode[] 0:62 as 0-9A-Za-z
Decode[] 0-9A-Za-z as 0:62
'''
for ix in range[ 0,10]: Encode[ix] = ix+ord['0']
for ix in range[10,36]: Encode[ix] = [ix-10]+ord['A']
for ix in range[36,62]: Encode[ix] = [ix-36]+ord['a']
for ix in range[ 0,10]: Decode[ix+ord['0']] = ix
for ix in range[10,36]: Decode[[ix-10]+ord['A']] = ix
for ix in range[36,62]: Decode[[ix-36]+ord['a']] = ix
def encode[x]:
'''
Encode[] 0:62 as 0-9A-Za-z
Otherwise '.'
'''
if x in Encode: return Encode[x]
# else: error
return ord['.']
def decode[x]:
'''
Decode[] 0-9A-Za-z as 0:62
Otherwise -1
'''
if x in Decode: return Decode[x]
# else: error
return -1
Các tập tin được sử dụng để lưu trữ dữ liệu vĩnh viễn. Xử lý tệp đang thực hiện các hoạt động khác nhau [đọc, viết, xóa, cập nhật, v.v.] trên các tệp này. Trong Python, quy trình xử lý tệp diễn ra trong các bước sau:
- Mở tệp
- Màn trình diễn
- Đóng tập tin
Có bốn chế độ cơ bản trong đó một tệp có thể được mở - đọc, ghi, nối và sáng tạo độc quyền. Ngoài ra, Python cho phép bạn chỉ định hai chế độ trong đó một tệp có thể được xử lý - nhị phân và văn bản. Chế độ nhị phân được sử dụng để xử lý tất cả các loại dữ liệu không phải là văn bản như tệp hình ảnh và tệp thực thi.
Viết các byte vào tệp trong Python
Ví dụ 1: Mở một tệp ở chế độ ghi nhị phân và sau đó chỉ định nội dung để ghi dưới dạng byte. Tiếp theo, sử dụng chức năng ghi để ghi nội dung byte vào tệp nhị phân. & Nbsp;Open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.
Python3
some_bytes
=
b
'\xC3\xA9'
with
open
[
write_array["data/filename",[1,2,3,4,5,6,7,8]]
0write_array["data/filename",[1,2,3,4,5,6,7,8]]
1write_array["data/filename",[1,2,3,4,5,6,7,8]]
2write_array["data/filename",[1,2,3,4,5,6,7,8]]
3write_array["data/filename",[1,2,3,4,5,6,7,8]]
4write_array["data/filename",[1,2,3,4,5,6,7,8]]
5Output:
my_file.txt
Ví dụ 2: Phương pháp này yêu cầu bạn tự thực hiện xử lý lỗi, nghĩa là đảm bảo rằng tệp luôn được đóng, ngay cả khi có lỗi trong khi viết. Vì vậy, bằng cách sử dụng các tuyên bố của người Viking về vấn đề này tốt hơn vì nó sẽ tự động đóng tệp khi khối kết thúc. This method requires you to perform error handling yourself, that is, ensure that the file is always closed, even if there is an error during writing. So, using the “with” statementis better in this regard as it will automatically close the file when the block ends.
Python3
some_bytes
=
b
write_array["data/filename",[1,2,3,4,5,6,7,8]]
9Encode = {}
Decode = {}
def EncodeInit[]:
'''
Encode[] 0:62 as 0-9A-Za-z
Decode[] 0-9A-Za-z as 0:62
'''
for ix in range[ 0,10]: Encode[ix] = ix+ord['0']
for ix in range[10,36]: Encode[ix] = [ix-10]+ord['A']
for ix in range[36,62]: Encode[ix] = [ix-36]+ord['a']
for ix in range[ 0,10]: Decode[ix+ord['0']] = ix
for ix in range[10,36]: Decode[[ix-10]+ord['A']] = ix
for ix in range[36,62]: Decode[[ix-36]+ord['a']] = ix
def encode[x]:
'''
Encode[] 0:62 as 0-9A-Za-z
Otherwise '.'
'''
if x in Encode: return Encode[x]
# else: error
return ord['.']
def decode[x]:
'''
Decode[] 0-9A-Za-z as 0:62
Otherwise -1
'''
if x in Decode: return Decode[x]
# else: error
return -1
0____4 open
[
write_array["data/filename",[1,2,3,4,5,6,7,8]]
0write_array["data/filename",[1,2,3,4,5,6,7,8]]
1__12write_array["data/filename",[1,2,3,4,5,6,7,8]]
5Encode = {}
Decode = {}
def EncodeInit[]:
'''
Encode[] 0:62 as 0-9A-Za-z
Decode[] 0-9A-Za-z as 0:62
'''
for ix in range[ 0,10]: Encode[ix] = ix+ord['0']
for ix in range[10,36]: Encode[ix] = [ix-10]+ord['A']
for ix in range[36,62]: Encode[ix] = [ix-36]+ord['a']
for ix in range[ 0,10]: Decode[ix+ord['0']] = ix
for ix in range[10,36]: Decode[[ix-10]+ord['A']] = ix
for ix in range[36,62]: Decode[[ix-36]+ord['a']] = ix
def encode[x]:
'''
Encode[] 0:62 as 0-9A-Za-z
Otherwise '.'
'''
if x in Encode: return Encode[x]
# else: error
return ord['.']
def decode[x]:
'''
Decode[] 0-9A-Za-z as 0:62
Otherwise -1
'''
if x in Decode: return Decode[x]
# else: error
return -1
9Output:
my_file.txt
Ví dụ 3: Ngoài ra, một số_bytes có thể ở dạng bytearray có thể thay đổi hoặc đối tượng byte là bất biến như hình dưới đây.Also, some_bytes can be in the form of bytearray which is mutable, or bytes object which is immutable as shown below.
Python3
some_bytes
0____4 some_bytes
2some_bytes
3some_bytes
4some_bytes
5some_bytes
4some_bytes
7some_bytes
4some_bytes
9=
0
some_bytes
=
=
3
=
4=
5
Encode = {}
Decode = {}
def EncodeInit[]:
'''
Encode[] 0:62 as 0-9A-Za-z
Decode[] 0-9A-Za-z as 0:62
'''
for ix in range[ 0,10]: Encode[ix] = ix+ord['0']
for ix in range[10,36]: Encode[ix] = [ix-10]+ord['A']
for ix in range[36,62]: Encode[ix] = [ix-36]+ord['a']
for ix in range[ 0,10]: Decode[ix+ord['0']] = ix
for ix in range[10,36]: Decode[[ix-10]+ord['A']] = ix
for ix in range[36,62]: Decode[[ix-36]+ord['a']] = ix
def encode[x]:
'''
Encode[] 0:62 as 0-9A-Za-z
Otherwise '.'
'''
if x in Encode: return Encode[x]
# else: error
return ord['.']
def decode[x]:
'''
Decode[] 0-9A-Za-z as 0:62
Otherwise -1
'''
if x in Decode: return Decode[x]
# else: error
return -1
7=
7=
=
9
with
open
[
write_array["data/filename",[1,2,3,4,5,6,7,8]]
0write_array["data/filename",[1,2,3,4,5,6,7,8]]
1write_array["data/filename",[1,2,3,4,5,6,7,8]]
2write_array["data/filename",[1,2,3,4,5,6,7,8]]
3write_array["data/filename",[1,2,3,4,5,6,7,8]]
4b
8Output::
my_file.txt
Ví dụ 4: Sử dụng mô -đun byte để ghi byte vào tệp Using the BytesIO module to write bytes to File
Python3
b
9 '\xC3\xA9'
0'\xC3\xA9'
1 '\xC3\xA9'
2
'\xC3\xA9'
3=
'\xC3\xA9'
5'\xC3\xA9'
6
Encode = {}
Decode = {}
def EncodeInit[]:
'''
Encode[] 0:62 as 0-9A-Za-z
Decode[] 0-9A-Za-z as 0:62
'''
for ix in range[ 0,10]: Encode[ix] = ix+ord['0']
for ix in range[10,36]: Encode[ix] = [ix-10]+ord['A']
for ix in range[36,62]: Encode[ix] = [ix-36]+ord['a']
for ix in range[ 0,10]: Decode[ix+ord['0']] = ix
for ix in range[10,36]: Decode[[ix-10]+ord['A']] = ix
for ix in range[36,62]: Decode[[ix-36]+ord['a']] = ix
def encode[x]:
'''
Encode[] 0:62 as 0-9A-Za-z
Otherwise '.'
'''
if x in Encode: return Encode[x]
# else: error
return ord['.']
def decode[x]:
'''
Decode[] 0-9A-Za-z as 0:62
Otherwise -1
'''
if x in Decode: return Decode[x]
# else: error
return -1
7with
open
[
with
1
write_array["data/filename",[1,2,3,4,5,6,7,8]]
1write_array["data/filename",[1,2,3,4,5,6,7,8]]
2with
4write_array["data/filename",[1,2,3,4,5,6,7,8]]
4with
6Output:
test.bin