Skip to content

Instantly share code, notes, and snippets.

@xgfs
Last active June 5, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xgfs/b283b6efc6cf0efd1bdc to your computer and use it in GitHub Desktop.
Save xgfs/b283b6efc6cf0efd1bdc to your computer and use it in GitHub Desktop.
How do i read that binary file
from struct import unpack
fname = r'D:\vk_1015\group_data_inv.bin'
buffer_size = 1024
last_node = 1
user_subs = {1: []}
stop = False
with open(fname, 'rb') as f:
while True:
bytes = f.read(4*buffer_size)
if len(bytes) == 0:
break
buffer_values = unpack('i'*(len(bytes)/4), bytes)
#print buffer_values
#break
for idx, value in enumerate(buffer_values):
if value <= 0:
last_node = -value
if last_node > max(user_subs.keys()):
stop = True
break
else:
if last_node in user_subs:
user_subs[last_node].append(value)
if stop:
break
del(bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment