import re,sys,codecs
from htmlentitydefs import name2codepoint

_entity_re = re.compile(r'&(?:(#)(\d+)|([^;]+));')

def _repl_func(match):
	if match.group(1): # Numeric character reference
		return unichr(int(match.group(2)))
	else:
		return unichr(name2codepoint[match.group(3)])

def handle_html_entities(string):
	return _entity_re.sub(_repl_func, string)

if __name__ == '__main__':
	if len(sys.argv) != 3:
		print "Usage: python strip_sepcial file_in file_out"
	else:
		out = codecs.open(sys.argv[1], 'w', 'utf8')
		for line in open(sys.argv[2]):
			out.write(handle_html_entities(line))
	
