#!/usr/bin/env python # -*- encoding: utf-8 -*- # count number of a-z in an article and print them # Copyright (c) 2006-2008 Pan Yongzhi import sys import collections if len(sys.argv) != 2: print 'Usage: count.py ' sys.exit() def print_frequency(data,c): w = data['Letters'] # whole number percentage = data[c]*100.0/w print '%s %.2f%%' % (c, percentage), return percentage def print_row(rows): print '\n' for i in rows: row_sum = 0 print '', for c in i: row_sum += print_frequency(data, c) print '' % row_sum print '\n
%.2f%%
' fd = open(sys.argv[1], 'r') data = collections.defaultdict(int) dv_rows = ['\',.pyfgcrl', 'aoeuidhtns', 'qjkxbmwvz'] qw_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm,.'] char = fd.read(1) while char: if char.isalpha(): # XXX data[char] += 1 data['Letters'] += 1 else: data['Others'] += 1 char = fd.read(1) print_row(dv_rows) print_row(qw_rows)