from collections import Counterwith open('a.txt', 'r+') as f: a = f.readlines() for i in range(len(a)): a[i] = a[i].strip().lower() b = [] a = dict(Counter(a)) for i in a.keys(): b.append('%s %d ' % (i, a[i]))f = open('a.txt', 'w')f.writelines(b)