Skip to content

Instantly share code, notes, and snippets.

@ourway
Last active January 1, 2016 17:09
Show Gist options
  • Save ourway/8174814 to your computer and use it in GitHub Desktop.
Save ourway/8174814 to your computer and use it in GitHub Desktop.
write arrays of numeric values to binary files
#!/usr/bin/env python
'''
@ref http://goo.gl/HgkAaG
@author Farsheed Ashouri
@usage: Give a folder of text files that contain 2d array of data:
5252,3434
4565,32123
...
'''
import array
import glob
import os
def convert_to_binary(folder):
'''
more info:
@ref: http://docs.python.org/2/library/array.html
'''
files = glob.glob(folder+os.path.sep+'*.lst')
for each in files:
f = open(each, 'r')
o = open(each+'.bin', 'wa')
for line in f.readlines():
x,y = line.split(',')
data = array.array('i', [int(x),int(y)])
data.tofile(o)
if __name__ == '__main__':
folder = 'data/tests'
convert_to_binary(folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment