How to find null byte in string in Python?

I have a problem with analyzing data after reading a file. What I am doing is reading a binary file, and I need to create a list of attributes from the read file, all data in the file ends with a zero byte. I am trying to find every attribute instance with a null byte character.

Essentially, taking the string as

Health\x00experience\x00charactername\x00 

and store it on a list.

The real problem is that I need to keep the null bytes in tact, I just need to be able to find each instance of the null byte and save the data that precedes it.

+5
source share
4 answers

While it comes down to using split('\x00') , a handy wrapper can be nice.

 def readlines(f, bufsize): buf = "" data = True while data: data = f.read(bufsize) buf += data lines = buf.split('\x00') buf = lines.pop() for line in lines: yield line + '\x00' yield buf + '\x00' 

then you can do something like

 with open('myfile', 'rb') as f: mylist = [item for item in readlines(f, 524288)] 

This has the added benefit of not having to load all the contents into memory until the text is split.

+4
source

Python does not treat NUL bytes as something special; they are no different from spaces or commas. So split() works fine:

 >>> my_string = "Health\x00experience\x00charactername\x00" >>> my_string.split('\x00') ['Health', 'experience', 'charactername', ''] 

Note that split treats \x00 as a delimiter, not as a terminator, so at the end we get an extra blank line. If this is a problem, you can simply cut it off:

 >>> my_string.split('\x00')[:-1] ['Health', 'experience', 'charactername'] 
+5
source

To check if a string has NULL bytes, simply use the in operator, for example:

 if b'\x00' in data: 

To find its position, use find() , which will return the lowest index in the row where the sub substring is. Then use the optional start and end arguments to slice notation.

+1
source

Divide by zero bytes; .split() returns a list:

 >> print("Health\x00experience\x00charactername\x00".split("\x00")) ['Health', 'experience', 'charactername', ''] 

If you know that data always ends with a zero byte, you can slice the list to cut the last empty line (for example, result_list[:-1] ).

0
source

All Articles