Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove some bytes from a byte string?

I am trying to remove a byte (\x00\x81) from a byte string sByte.

sByte = b'\x00\x81308 921 q53 246 133 137 022 1   0 1 1  1 130 C13 330 0000000199 04002201\n'

I am expecting to have as a result the following:

sByte = b'308 921 q53 246 133 137 022 1   0 1 1  1 130 C13 330 0000000199 04002201\n'

I have tried the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. I tried to decode sByte; after running the below line of code,

    sByte.decode('utf-8')
    

    I received a traceback: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 1: invalid start byte.

  2. I also tried the following, but did not work:

    sByte.replace('\x00\x81', '')
    
  3. I also found this:
    json – UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0x80 in position 3131: invalid start byte) but it did not help removing \x00\x81.

I am not sure how we can remove or replace a byte in byte string.

>Solution :

bytes.replace doesn’t work in-place, it returns a modified copy of the bytes object. You can use sByte = sByte.replace(b'\x00\x81', b'') (or bytes.removeprefix if the bytes always occur at the start). Depending on your circumstances, you can also set the errors parameter of the decode method to 'ignore': sByte = sByte.decode(encoding='utf-8', errors='ignore').

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading