#!/usr/bin/env python # -*- encoding: utf-8 -*- # Count numbers of a-z in an article and print them # Copyright (c) 2006-2011 Pan Yongzhi # Python version: <= 2.7 from __future__ import division import sys import collections if len(sys.argv) != 2: print 'Usage: count.py ' sys.exit() dv_rows = ["',.pyfgcrl", 'aoeuidhtns', 'qjkxbmwvz'] qw_rows = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm,.'] fd = open(sys.argv[1], 'r') data = collections.defaultdict(int) char = fd.read(1) while char: if char in ''.join(dv_rows): data[char] += 1 data['keys'] += 1 char = fd.read(1) for rows in dv_rows, qw_rows: print '' print '' for row in rows: row_sum = 0 print '' for c in row: percent = data[c]/data['keys']*100 print '' % (c, percent) row_sum += percent print '' % row_sum print '' print '' print '
%s %.2f%%%.2f%%
'