Python Challenge [2]

Using ‘char’ string function to look through a text for rare characters.

As an introduction to the world of text processing, we’re given a mighty text of gibberish to work with. There seems to be a whole mess of stuff in the page source and we’re supposed to find rare characters. First, save the text to a file and then get ‘mess’ as string.

import os
mess = open("mess.txt").read()

Then we’ll try building an array with a character and that character’s count.

charCount = {}

for char in mess:    
	charCount[char] = charCount.get(char, 0) + 1 

value_key = sorted([(v, k) for k, v in charCount.items()], reverse=True)
for vk in value_key:
	if vk[1] not in "\n":
		print "%2s %3d" % (vk[1], vk[0])