Writing unicode strings via sys.stdout in Python

后端 未结 5 870
悲哀的现实
悲哀的现实 2020-11-30 02:22

Assume for a moment that one cannot use print (and thus enjoy the benefit of automatic encoding detection). So that leaves us with sys.stdout. Howe

5条回答
  •  一生所求
    2020-11-30 02:30

    export PYTHONIOENCODING=utf-8
    

    will do the job, but can't set it on python itself ...

    what we can do is verify if isn't setting and tell the user to set it before call script with :

    if __name__ == '__main__':
        if (sys.stdout.encoding is None):
            print >> sys.stderr, "please set python env PYTHONIOENCODING=UTF-8, example: export PYTHONIOENCODING=UTF-8, when write to stdout."
            exit(1)
    

提交回复
热议问题