Python Forum

Full Version: looking 4 py code: file byte/character frequency
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
the command line argument would be a filename in the coding used by the host system.  if the given name is what the platform normally uses to designate reading standard input or is missing, then the input will be stdin.  each byte/character of input will be counted, based on separate counters for each value.  output will be after end of file on input.  output will spell out the frequency of each byte/character of input and will show that frequency in some order sorted either by the byte/character value or the frequency count.

the code should work under python 3 as a minimum.  it can be made to work under both python 2 and python 3 by any known method of doing that (such as version checks), or by having separate code for each.
Actually, collections.Counter do just that. Count the characters. Don't know for non printable ones.
I have somewhere a piece of code I've wrote for a yet unfinished project during my basic Python learning. It counts and sort by decr. frequency

Didn't know the collections module that time
i believe collections.Counter works for anything that is hashable, just like a dictionary.

FYI, i am looking for done code.  in many cases i already know how and am looking for coding ideas like what issues the needs to handle.  i've already used collections.Counter in a couple projects.