How to remove those "\x00\x00" in a string ? I have many of those strings (example shown below). I can use re.sub
to replace those "\x00". But I am wondering whether there is a better way to do that? Converting between unicode, bytes and string is always confusing.
'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'.
Use the str. replace() method to remove \x00 from a string, e.g. result = my_str. replace('\x00', '') .
\x is used to denote an hexadecimal byte. \x00 is thus a byte with all its bits at 0. (As Ryne pointed out, a null character translates to this.)
Use rstrip
>>> text = 'Hello\x00\x00\x00\x00' >>> text.rstrip('\x00') 'Hello'
It removes all \x00
characters at the end of the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With