Added fix for Issue#977
changed line to if PY3
made the encode fix at line 861
Merge pull request #986 from jdnc/bugfix-issue-977

Added fix to close #977