view char_hist.py @ 51:44fea514ca45

foo
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sun, 19 Feb 2023 16:44:06 +0000
parents
children
line wrap: on
line source

#!/usr/bin/python3
import sys

h={}
mc=0
nonAscii=0

if len(sys.argv)>1:
  showMe=[int(a) for a in sys.argv[1:]]
else:
  showMe=[]

def main():
  global h, mc, nonAscii, showMe
  for l in sys.stdin:
    na=0
    sm=0
    for c in l:
      h[c]=h.get(c,0)+1
      o=ord(c)
      sm+=(o in showMe)
      na+=(o>127)
      if o>mc:
        mc=o
    if na>0:
      nonAscii+=1
    if sm>0:
      sys.stderr.write(l)

  for i in range(128):
    print(i,chr(i),h.get(chr(i),0))
  print('-------')
  if nonAscii>0:
    print("%s lines with one or more non-ascii characters"%nonAscii)
  for i in range(128,mc+1):
    c=chr(i)
    if c in h:
      print(i,c,h[c])

if __name__ == '__main__':
  main()