Skip to content

Instantly share code, notes, and snippets.

@mjpost
Last active July 14, 2025 04:35
Show Gist options
  • Select an option

  • Save mjpost/0b44805f3cacdb92bee7 to your computer and use it in GitHub Desktop.

Select an option

Save mjpost/0b44805f3cacdb92bee7 to your computer and use it in GitHub Desktop.

Revisions

  1. mjpost revised this gist Sep 10, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions unicode_header.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    # Python *sucks* at UTF-8 (don't tell me "It's fixed in Python 3"; I don't care, plus no one uses Python 3)
    # If you put this at the top of every Python script, however, it get rids of most of the headaches dealing with STDIN
    # and STDOUT (basically, akin to "perl -C31"). I don't know if it's all necessary; I just know that if I put it at
  2. mjpost created this gist Jul 2, 2015.
    13 changes: 13 additions & 0 deletions unicode_header.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Python *sucks* at UTF-8 (don't tell me "It's fixed in Python 3"; I don't care, plus no one uses Python 3)
    # If you put this at the top of every Python script, however, it get rids of most of the headaches dealing with STDIN
    # and STDOUT (basically, akin to "perl -C31"). I don't know if it's all necessary; I just know that if I put it at
    # the top of my scripts, most of the problems go away, and I can stop thinking about it.

    import sys
    import codecs

    reload(sys)
    sys.setdefaultencoding('utf-8')
    sys.stdin = codecs.getreader('utf-8')(sys.stdin)
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
    sys.stdout.encoding = 'utf-8'