In [1]:
# convert ASCII binary data in a given CSV into binary file, import modules
from cStringIO import StringIO
import binascii

In [2]:
def int2bin(fpbits):
    sio = StringIO(fpbits)
    outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    f = open(outfile, 'wb')
    # when condition is true (1)
    while 1:
        # get the next 8 bits
        b = sio.read(8)
        # CHECK THE READING STRING
        #print (b) 
        # check if hit EOF
        if not b:
            break
        # If less than 8 bits, pad with zeroes (0's) on the right
        if len(b) < 8:
            b = b + '0' * (8 - len(b))
            print (b)
        # Convert to integer (int)
        i = int(b, 2)
        # Convert to character (chr)
        c = chr(i)
        # Write the file
        f.write(c)
    f.close()
    return f

In [13]:
ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/example-words-bin.txt"
# open file in read write mode of binary
fout = open('output.bin','w+b')
fpbits=[]
with open(ifile,"r") as f:
    for l in f:
        fpbits.append(l)
        l=map(str.rstrip,l)
        m = ''.join(l)
        fpfile = int2bin(m)
        fpfile1 = open (fpfile.name, 'rb')
        fout.write(fpfile1.read())
        print (fout.read())
    fout.close()






In [ ]:


In [ ]: