search

Home  >  Q&A  >  body text

python - 为什么在首行定义了utf-8,还要用sys再set一次?

代码如下:

#-*- coding:utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

为什么已经写了#-*- coding:utf-8 -*-
下面还要sys.setdefaultencoding('utf-8')
求教?谢谢


已解决
这里说的挺明白的:也谈 Python 的中文编码处理

ringa_leeringa_lee2841 days ago799

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:37:59

    The above one refers to the encoding of this file. The root python sys has nothing to do with it.

    reply
    0
  • PHPz

    PHPz2017-04-17 16:37:59

    Why we need sys.setdefaultencoding(“utf-8”) in a py script?

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:37:59

    You can import sys in python. Then use sys.getdefaultencoding() to see what the default encoding of your python is. Anyway, my default is ascii...
    By the way, I used the lines of your code on Ubuntu yesterday, and the Chinese output to the command line is still garbled... slightly Weird... In the end, I processed Chinese directly in the file

    reply
    0
  • Cancelreply