Search code examples
pythonsplithexcut

Python - split file based on HEX values


I have a file that I want to process, I want to identify specific HEX values and split the file, then save based on this (including the HEX identifier). The HEX value is 4 bytes i.e OA FF AA 1B

So the first part of the file is a Header record, the 2nd part including the HEX identifier i want to cut and save to disk.

I been trying to use Seek() to achieve a solution, but not sure this is the correct module to use.

Any help appreciated


Solution

  • Based on your description I think this piece of code can represent some hints to you:

    In [1]: delimiter = '0x32a'   # whatever the HEX value is                                                                                                                                                 
    
    In [2]: with open("filename.txt", "r") as f: 
       ...:     data = f.read() 
       ...:     chunks = data.split(delimiter) 
       ...: