How to remove these "\ x00 \ x00"

How to remove these "\ x00 \ x00" in a line? I have many such lines (an example is shown below). I can use re.sub to replace these "\ x00". But I wonder if there is a better way to do this? Conversion between unicode, bytes and string is always confused.

'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'.

+6
source share
4 answers

Use rstrip

>>> text = 'Hello\x00\x00\x00\x00'
>>> text.rstrip('\x00')
'Hello'

It deletes all characters \x00at the end of the line.

+15
source
>>> a = 'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 
>>> a.replace('\x00','')
'Hello'
+15
source

, :

cleanstring = nullterminatedstring.split('\x00',1)[0]

split \x00 1. split(...) 2- : ( ). [0] null (\ x00), , , , .

, , C-like, , . , , :

'Hello\x00dpiecesofsomeoldstring\x00\x00\x00'

, , , .

+2

, , strip() , rstrip() , strip() , rstrip() .

However, by default, NUL characters are not treated as strip () as white space characters, so you need to specify them explicitly. This may catch you, since print (), of course, will not show NUL characters. My solution that I used was to clear the string using " .strip().strip('\x00')":

>>> arbBytesFromSocket = b'\x00\x00\x00\x00hello\x00\x00\x00\x00'
>>> arbBytesAsString = arbBytesFromSocket.decode('ascii')
>>> print(arbBytesAsString)
hello
>>> str(arbBytesAsString)
'\x00\x00\x00\x00hello\x00\x00\x00\x00'
>>> arbBytesAsString = arbBytesFromSocket.decode('ascii').strip().strip('\x00')
>>> str(arbBytesAsString)
'hello'
>>>

This gives you the required string without NUL characters.

0
source

Source: https://habr.com/ru/post/1650843/


All Articles