Hướng dẫn python combine binary data - python kết hợp dữ liệu nhị phân

Tôi đang gặp vấn đề về dữ liệu vào một tệp nhị phân. Khi tôi seek[] đến một vị trí, sau đó write[] tại vị trí đó và sau đó đọc toàn bộ tệp, tôi thấy rằng dữ liệu không được viết tại vị trí mà tôi muốn. Thay vào đó, tôi tìm thấy nó ngay sau mỗi dữ liệu/văn bản khác.

Nội dung chính ShowShow

  • Làm thế nào để bạn sửa đổi một tệp nhị phân trong Python?
  • Làm thế nào để bạn chèn dữ liệu vào một tệp python?
  • Làm thế nào để bạn viết dữ liệu trong một tệp nhị phân?
  • Dữ liệu được lưu trữ trong tệp nhị phân trong Python như thế nào?

Mã của tôi

file = open['myfile.dat', 'wb']
file.write['This is a sample']
file.close[]

file = open['myfile.dat', 'ab']
file.seek[5]
file.write[' text']
file.close[]

file = open['myfile.dat', 'rb']
print file.read[]  # -> This is a sample text

Bạn có thể thấy rằng seek không hoạt động. Làm thế nào để tôi giải quyết điều này? Có những cách khác để đạt được điều này?

Cảm ơn

wjandrea

24.8k8 Huy hiệu vàng53 Huy hiệu bạc73 Huy hiệu đồng8 gold badges53 silver badges73 bronze badges8 gold badges53 silver badges73 bronze badges

Hỏi ngày 8 tháng 12 năm 2010 lúc 13:50Dec 8, 2010 at 13:50Dec 8, 2010 at 13:50

1

Trên một số hệ thống, 'ab' buộc tất cả các ghi phải xảy ra ở cuối tệp. Bạn có thể muốn 'r+b'.

Đã trả lời ngày 8 tháng 12 năm 2010 lúc 13:54Dec 8, 2010 at 13:54Dec 8, 2010 at 13:54

Marcelo Cantosmarcelo CantosMarcelo CantosMarcelo Cantos

177K38 Huy hiệu vàng321 Huy hiệu bạc362 Huy hiệu Đồng38 gold badges321 silver badges362 bronze badges38 gold badges321 silver badges362 bronze badges

4

R+B nên hoạt động như bạn muốn

Đã trả lời ngày 8 tháng 12 năm 2010 lúc 14:26Dec 8, 2010 at 14:26Dec 8, 2010 at 14:26

Để lại lệnh tìm kiếm. Bạn đã mở tệp để nối thêm 'A'.

Đã trả lời ngày 8 tháng 12 năm 2010 lúc 13:56Dec 8, 2010 at 13:56Dec 8, 2010 at 13:56

PaulpaulPaulPaul

4.7243 huy hiệu vàng28 Huy hiệu bạc38 Huy hiệu đồng3 gold badges28 silver badges38 bronze badges3 gold badges28 silver badges38 bronze badges

3

Lưu ý: Ghi nhớ các byte mới qua các byte trước đó

Theo Python 3 Cú pháp

with open['myfile.dat', 'wb'] as file:
    b = bytearray[b'This is a sample']
    file.write[b]

with open['myfile.dat', 'rb+'] as file:
    file.seek[5]
    b1 = bytearray[b'  text']
    #remember new bytes over write previous bytes
    file.write[b1]

with open['myfile.dat', 'rb'] as file:
    print[file.read[]]

Đầu ra

b'This   textample'

Hãy nhớ các byte mới hơn ghi byte trước đó

wjandrea

24.8k8 Huy hiệu vàng53 Huy hiệu bạc73 Huy hiệu đồng8 gold badges53 silver badges73 bronze badges8 gold badges53 silver badges73 bronze badges

Hỏi ngày 8 tháng 12 năm 2010 lúc 13:50Dec 8, 2010 at 13:50Jul 29, 2019 at 11:19

3

Trên một số hệ thống, 'ab' buộc tất cả các ghi phải xảy ra ở cuối tệp. Bạn có thể muốn 'r+b'.Binary files are always written in structure form and later it is converted to byte stream before storing to binary file. By structure from I mean to say in formats like lists, tuple, sets, dictionary etc.

Đã trả lời ngày 8 tháng 12 năm 2010 lúc 13:54Dec 8, 2010 at 13:54As we know that binary files written in structure form list, tuple etc. So, to append any new data to our binary file we need to use append[] function same as we did in writing data to binary file in python. Just only difference is the file mode. We use “ab” for appending data to our Binary File.


Marcelo Cantosmarcelo CantosMarcelo Cantos
 
Basically our requirement is that we will keep on asking to user to enter records till he was not done with entering records. We also need to keep in mind that we have to perform operation without deleting or over writing previous records that was already written in file.

177K38 Huy hiệu vàng321 Huy hiệu bạc362 Huy hiệu Đồng38 gold badges321 silver badges362 bronze badgesI have a file “bin.dat” storing some records of students.

R+B nên hoạt động như bạn muốnbin.dat


Đã trả lời ngày 8 tháng 12 năm 2010 lúc 14:26Dec 8, 2010 at 14:26


Để lại lệnh tìm kiếm. Bạn đã mở tệp để nối thêm 'A'.
Here our program is same as that we did in writing multiple data to Binary File. But the only difference is that here we have used “ab” file mode. But in our previous program [writing multiple data to Binary File] we used “wb” file mode.

Đã trả lời ngày 8 tháng 12 năm 2010 lúc 13:56Dec 8, 2010 at 13:56

PaulpaulPaulHere we have opened our already existing file “bin.dat” in append mode for adding more records to our binary file.

Bước 2: & nbsp; Chúng tôi đã tạo một danh sách trống rec [] trong đó chúng tôi nối dữ liệu và sau đó chúng tôi gửi dữ liệu đó vào tệp của chúng tôi.We created an empty list rec[] in which we append data and later we dump that data to our file.We created an empty list rec[] in which we append data and later we dump that data to our file.

Bước a: & nbsp; Đây là vòng lặp while của chúng tôi [] theo mặc định được đặt thành true, để người dùng có thể nhập vào while loop [] để nối dữ liệu.Here is the our while loop[] which is by default set to True, so that user can enter to while loop[] to append data.Here is the our while loop[] which is by default set to True, so that user can enter to while loop[] to append data.

Bước 3: & nbsp; Ở bước này, chúng tôi chỉ lấy chi tiết về sinh viên làm đầu vào.At this step, we are only taking details of student as an input.At this step, we are only taking details of student as an input.

Bước 4: & nbsp; Ở đây chúng tôi đã tạo một dữ liệu danh sách mới. Trong danh sách này, chúng tôi đang lưu trữ các đầu vào mà chúng tôi đã lấy từ người dùng.Here we have created a new list data. In this list we are storing the inputs that we have taken from user.Here we have created a new list data. In this list we are storing the inputs that we have taken from user.

Bước 5: & nbsp; Ở đây chúng tôi đang thêm dữ liệu danh sách của mình [] đang lưu trữ các đầu vào được cung cấp bởi người dùng dưới dạng chi tiết của sinh viên vào danh sách trước đây của chúng tôi [].Here we are appending our list data[] which is storing the inputs given by user as details of student to our previous list rec[].Here we are appending our list data[] which is storing the inputs given by user as details of student to our previous list rec[].

Bước 6: & NBSP; Bây giờ, hồ sơ mới của sinh viên đã được thêm vào hồ sơ của chúng tôi. Ở đây tại Bước 6, chúng tôi sẽ yêu cầu người dùng nhập lựa chọn của mình dưới dạng Y/N. để tiếp tục viết dữ liệu hoặc ngừng viết thêm dữ liệu.Now the new record of student has been appended to our file. Here at step 6, we will ask user to enter his choice as Y/N . to continue to writing data or to stop writing more data.Now the new record of student has been appended to our file. Here at step 6, we will ask user to enter his choice as Y/N . to continue to writing data or to stop writing more data.

Bước 7: & nbsp; ở đây điều kiện được kiểm tra xem người dùng đã nhập Y/N như một lựa chọn. Nếu lựa chọn được nhập là n thì vòng lặp sẽ ngừng thực thi. Và thoát khỏi vòng lặp trong khi.Here condition is checked that user entered Y/N as a choice. If choice entered is N then loop will stop executing. And exit the while loop.Here condition is checked that user entered Y/N as a choice. If choice entered is N then loop will stop executing. And exit the while loop.

Bước 8: & nbsp; Ở đây chúng tôi đang thêm đầu vào dữ liệu được lấy từ người dùng vào tệp nhị phân của chúng tôi.Here we are appending the data input taken from user to our binary file “bin.dat”.Here we are appending the data input taken from user to our binary file “bin.dat”.

Bước 9: & nbsp; Đây là định nghĩa chức năng của việc đọc tất cả các bản ghi từ tệp nhị phân của chúng tôi.This is the function definition of reading all records from our binary file “bin.dat”.This is the function definition of reading all records from our binary file “bin.dat”.

Bước 10: Ở đây chúng tôi đang gọi hàm đọc [] của chúng tôi.Here we are calling our read[] function.Here we are calling our read[] function.

Do đó, chúng tôi có thể nối dữ liệu vào tệp nhị phân của chúng tôi.

Làm thế nào để bạn sửa đổi một tệp nhị phân trong Python?

Bước 1: Tìm kiếm từ trong tệp nhị phân. Bước 2: Trong khi tìm kiếm trong tệp, biến có thể lưu trữ vị trí của bản ghi con trỏ tệp sau đó đọc qua [tiếp tục] đọc bản ghi. Bước 3: Nếu từ được tìm kiếm tồn tại thì hãy đặt con trỏ ghi [để kết thúc bản ghi trước đó] tức là tại POS.

Làm thế nào để bạn chèn dữ liệu vào một tệp python?

Chụp dữ liệu vào một tệp như một dòng mới trong Python...

Mở tệp ở chế độ phụ lục ['A']. Viết điểm con trỏ vào cuối tệp ..

Nối '\ n' ở cuối tệp bằng hàm write [] ..

Nối dòng đã cho vào tệp bằng hàm write [] ..

Đóng tệp ..

Làm thế nào để bạn viết dữ liệu trong một tệp nhị phân?

fwrite [fileID, a] ghi các phần tử của mảng A AS 8 bit số nguyên không dấu vào một tệp nhị phân theo thứ tự cột.Tệp nhị phân được biểu thị bằng nhận dạng tệp, FileID.Sử dụng Fopen để mở tệp và lấy giá trị FileID.Khi bạn viết xong, hãy đóng tệp bằng cách gọi Fclose [FileID].. The binary file is indicated by the file identifier, fileID . Use fopen to open the file and obtain the fileID value. When you finish writing, close the file by calling fclose[fileID] .. The binary file is indicated by the file identifier, fileID . Use fopen to open the file and obtain the fileID value. When you finish writing, close the file by calling fclose[fileID] .

Dữ liệu được lưu trữ trong tệp nhị phân trong Python như thế nào?

Các tệp nhị phân cũng được lưu trữ theo các byte [0 và 1], nhưng không giống như các tệp văn bản, các byte này không đại diện cho các giá trị ASCII của các ký tự.in terms of bytes [0s and 1s], but unlike text files, these bytes do not represent the ASCII values of characters.in terms of bytes [0s and 1s], but unlike text files, these bytes do not represent the ASCII values of characters.

Bài Viết Liên Quan

Chủ Đề