bf17 icon

Byte frequency of text or binary file

bf17 | PRO | 04/12/17 12:06:15 PM UTC | 0 ⭐ | 400 👁️ | Never ⏰ | []
Python |

580 B

|

None

|

0 👍

/

0 👎

#python 3.5
# cf1 - 1st order character distribution
# text or binary files
 
#set up array
from array import array
a = array("H")
 
# ASCII
def printable(n):
    if((n>31 and n<127)):
        return(True)
    else:
        return(False)
 
input_file = input("file name: ")
 
f = open(input_file, 'rb')
 
# creat and initialize array
a = [0 for i in range(256)]
 
while True:
    nextch = f.read(1)
    if(not nextch): break
    
    a[ord(nextch)] += 1
    
for i in range(256):
    if(a[i] > 0):
        if(printable(i)):
            print("'" + chr(i) + "'", "\t", a[i])
        else:
            print(i, "\t", a[i])
 
f.close()

Comments