Hướng dẫn modify binary file python - sửa đổi tệp nhị phân python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    Step 1: Searching for the word in the binary file. 
    Step 2: While searching in the file, the variable “pos” stores the position of file pointer record then traverse(continue) reading of the record. 
    Step 3: If the word to be searched exists then place the write pointer (to ending of the previous record) i.e. at pos. 
    Step 4: Call write() function to take the new record. 
    Step 5: Write the new object at the position “pos” and hence the record is updated and print “record successfully updated”. 
    Step 6: If the word does not exists then print “record not found”.

    Đưa ra một tệp nhị phân có chứa một số câu (các từ được phân tách không gian), hãy để viết một chương trình Python để sửa đổi hoặc thay đổi bất kỳ từ cụ thể nào của câu.
    Let’s suppose the content of the binary file is:

    Hướng dẫn modify binary file python - sửa đổi tệp nhị phân python

    Python3

    Cách tiếp cận: Bước 1: Tìm kiếm từ trong tệp nhị phân. & NBSP; Bước 2: Trong khi tìm kiếm trong tệp, biến POS POS 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. & NBSP; 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 đó) Đối tượng tại vị trí của POS POS và do đó bản ghi được cập nhật và in bản ghi được cập nhật thành công.

    Triển khai, giả sử nội dung của tệp nhị phân là:

    def update_binary(word, new)

        update_binary(word, new)1update_binary(word, new)2update_binary(word, new)3update_binary(word, new)4update_binary(word, new)5update_binary(word, new)6update_binary(word, new)7update_binary(word, new)8update_binary(word, new)9

        string = b""

        Flag ____4 0

        0    1= 0

        0    5= string = update_binary(word, new)8string 0string 1string 2

        0string 4 string 5

    string 6    5= update_binary(word, new)8string 0string 1string 2

    b""7update_binary(word, new)8b""9

    b""7update_binary(word, new)8    2

    string 6=4     5== =8=9update_binary(word, new)9

    b""7    8

    b""1Flag 0update_binary(word, new)9

    b""1=4 string == b""6

    b""7Flag ____4 string 1

    string 6Flag 0update_binary(word, new)9

    b""7    1= update_binary(word, new)8Flag 6

    b""105

    b""7    5= string = update_binary(word, new)8string 0string 1string 2

        0update_binary(word, new)00update_binary(word, new)3update_binary(word, new)02string 2

        Flag 0update_binary(word, new)9

        0update_binary(word, new)00update_binary(word, new)3update_binary(word, new)10string 2

    b""1string 01= 03

        =4 08

    update_binary(word, new)

    Output:

    Hướng dẫn modify binary file python - sửa đổi tệp nhị phân python

    update_binary(word, new)12= update_binary(word, new)14update_binary(word, new)3update_binary(word, new)16update_binary(word, new)17

    Hướng dẫn modify binary file python - sửa đổi tệp nhị phân python