MKM2
MKM2

Reputation: 31

Delete some specific content from byte in python 3

I have no idea of how to delete '\x00' from the following byte.Currently, I am trying to write this byte to a text file in python 3.

b'Today, in the digital age, any type of data, such as text, images, and 
audio, can be\r\ndigitized, stored indefinitely, and transmitted at high 
speeds. Notwithstanding these\r\nadvantages, digital data also have a 
downside. They are easy to access illegally, tamper\r\nwith, and copy for 
purposes of copyright violation.\r\nThere is therefore a need to hide secret 
identification inside certain types of digital\r\ndata. This information can 
be used to prove copyright ownership, to identify attempts\r\nto tamper with 
sensitive data, and to embed annotations. Storing, hiding, or embedding\r
\nsecret information in all types of digital data is one of the tasks of the 
field of\r\nsteganography.\r\nSteganography is the art and science of data 
hiding. In contrast with cryptography,\r\nwhich secures data by transforming 
it into another, unreadable format, steganography\r\nmakes data invisible by 
hiding (or embedding) them in another piece of data, known\r\nalternatively as
 the cover, the host, or the carrier. The modified cover, including 
the\r\nhidden data, is referred to as a stego object. It can be stored or
 transmitted as a message.\r\nWe can think of cryptography as overt secret 
writing and of steganography as covert\r\nsecret writing.\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00'

I want to delete multiple \x00 from the end of the sentence. Please help me!

Upvotes: 2

Views: 11067

Answers (2)

Reti43
Reti43

Reputation: 9796

Bytes objects behave like many other iterables, which means slicing and indexing should work as expected. Since the character you want to remove is specifically at the end and the object supports the method, the solution is the same as in striping characters from the end of a string. Just make sure to pass the desired characters are bytes.

>>> my_bytes = b'blah\x00\x00\x00'
>>> my_bytes.rstrip(b'\x00')
b'blah'

Upvotes: 2

Patrick Haugh
Patrick Haugh

Reputation: 60944

Use bytes.replace to replace the substring with an empty string:

b = b'Today, in the digital age, any type of data, such as text, images, and audio, can be\r\ndigitized, stored indefinitely, and transmitted at high speeds. Notwithstanding these\r\nadvantages, digital data also have a downside. They are easy to access illegally, tamper\r\nwith, and copy for purposes of copyright violation.\r\nThere is therefore a need to hide secret identification inside certain types of digital\r\ndata. This information can be used to prove copyright ownership, to identify attempts\r\nto tamper with sensitive data, and to embed annotations. Storing, hiding, or embedding\r\nsecret information in all types of digital data is one of the tasks of the field of\r\nsteganography.\r\nSteganography is the art and science of data hiding. In contrast with cryptography,\r\nwhich secures data by transforming it into another, unreadable format, steganography\r\nmakes data invisible by hiding (or embedding) them in another piece of data, known\r\nalternatively as the cover, the host, or the carrier. The modified cover, including the\r\nhidden data, is referred to as a stego object. It can be stored or transmitted as a message.\r\nWe can think of cryptography as overt secret writing and of steganography as covert\r\nsecret writing.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

b = b.replace(b'\x00', b'')
assert b.endswith(b'writing.')

Upvotes: 3

Related Questions