Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove those "\x00\x00"

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'. 
like image 275
Luffy Cyliu Avatar asked Aug 10 '16 21:08

Luffy Cyliu


People also ask

How do I get rid of x00 in Python?

Use the str. replace() method to remove \x00 from a string, e.g. result = my_str. replace('\x00', '') .

What is <UNK>x00 C?

\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.)


1 Answers

Use rstrip

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

It removes all \x00 characters at the end of the string.

like image 182
warownia1 Avatar answered Sep 18 '22 06:09

warownia1