Berman Berman - 6 months ago 14
Python Question

How to get rid of \x00 in my array of bytes?

Here is the example of my array:


b'[\x00{\x00"\x00s\x00u\x00b\x00i\x00d\x00"\x00:\x006\x002\x008\x003\x007\x00,\x00"\x00n\x00a\x00m\x00e\x00"\x00:\x00"\x00B\x00e\x00y\x00o\x00n\x00d\x00\x00E\x00y\x00e\x00s\x00"\x00,\x00"\x00p\x00r\x00i\x00c\x00e\x00"\x00:\x00"\x001\x001\x008\x00\x00p\x00C\x041\x04.\x00"\x00,\x00"\x00l\x00i\x00n\x00k\x00"\x00:\x00"\x00a\x00p\x00p\x00/\x003\x005\x006\x000\x005\x000\x00"\x00}\x00]\x00'



is there any way to get rid of all the null characters? Any kind of replacing isn't the solution because the other symbols start with
\x00

Answer

Decode it to text. Or open the file with the correct encoding in the first place.

>>> b'[\x00{\x00"\x00s\x00u\x00b\x00'.decode('utf-16le')
'[{"sub'