PEP8 cleanup and fix for extra lines in CSV output.
PEP8 cleanup plus encoding fix.