migrated code to be Python 3 compatible and added a context manager to t...

Had to do some changes for string/unicode compat as well as generators (.keys() and .values() are generators in 3).

Context manager is there too and also fixed open() files and files descriptors and made the passing of a file descriptor/string to outfile versus a more readable
Nick