Skip to content

Instantly share code, notes, and snippets.

@circulosmeos
Created April 15, 2018 22:30
Show Gist options
  • Save circulosmeos/ec550ad598569053f795c7e9e46947cd to your computer and use it in GitHub Desktop.
Save circulosmeos/ec550ad598569053f795c7e9e46947cd to your computer and use it in GitHub Desktop.
convert binary file //headers.electrum.org/blockchain_headers to CSV ASCII
#!/usr/bin/env python
# convert binary file http://headers.electrum.org/blockchain_headers
# to CSV ASCII
import binascii
STRUCT_OF_BLOCK = [ 4, 32, 32, 4, 4, 4 ] # blockchain_headers does not contain always "0x00" txn_count
BLOCK_SIZE = sum(STRUCT_OF_BLOCK)
FILE_OUT= open('blockchain_headers.csv','w')
FILE_OUT.write( "version,prev_block,merkle_root,timestamp,bits,nonce,txn_count\n" )
with open('blockchain_headers','rb') as FILE:
block = FILE.read(BLOCK_SIZE)
while block != b'':
position = 0
for i in STRUCT_OF_BLOCK:
FILE_OUT.write( bytearray(binascii.hexlify( block[position:(position+i)][::-1] )).decode('ascii') + ',')
position += i
if position >= BLOCK_SIZE:
FILE_OUT.write("00\n") # blockchain_headers does not contain always "0x00" txn_count
block = FILE.read(BLOCK_SIZE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment