Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- # with open('test.bin', 'bw') as file:
- # file.write(b'Food\x01Fruit\x02Banana\x03Apple\x03Vegetable\x02Carrot\x03')
- def insert(tree, path):
- node = tree
- for key in path:
- if not (key in node):
- node[key] = {}
- node = node[key]
- def parse_file(filename):
- pattern = re.compile(b'([a-zA-Z]+)([\x01-\x1F])')
- result = {}
- tree_path = []
- with open(filename, 'rb') as file:
- data = file.read()
- for key, level in re.findall(pattern, data):
- tree_path[ord(level):] = [key]
- insert(result, tree_path)
- return result
- assert parse_file('test.bin') == {b'Food': {b'Fruit': {b'Vegetable': {b'Carrot': {}}, b'Banana': {b'Apple': {}}}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement