Last active
January 1, 2016 17:09
-
-
Save ourway/8174814 to your computer and use it in GitHub Desktop.
write arrays of numeric values to binary files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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