Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 2
    My context was running the Python script as a CGI under Apache, where the default output encoding wasn't what I needed (I needed UTF-8). I think it's better for the script to ensure that its output is in the correct encoding, rather than relying on external settings (such as environment variables like PYTHONIOENCODING). Commented Dec 7, 2010 at 10:03
  • 1
    Yet another proof that using stdout for process communication is big mistake. I realize you may have no choice than to use CGI in this case though so that's not your fault. :-) Commented Dec 7, 2010 at 11:45
  • 2
    While it is true that sys.stdout is a binary file in Python 2 and a text file in Python 3, I think your Python 2 example fails because the unicode string u"ûnicöde" that gets implicitly encoded in the sys.stdout.write method has characters outside the ASCII range. If you change your LC_CTYPE, LANG or PYTHONIOENCODING environment variables to an encoding that has all the characters in the unicode string you should not get any error. (I have tried on Python 2.7.) Commented Feb 22, 2018 at 8:43