python - 为什么在首行定义了utf-8,还要用sys再set一次?
ringa_lee
ringa_lee 2017-04-17 16:35:59
0
3
733

代码如下:

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

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

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


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

ringa_lee
ringa_lee

ringa_lee

reply all(3)
巴扎黑

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

PHPzhong

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

黄舟

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!