xywuyiba8 发表于 2017-4-22 14:29:39

Python 使用 UTF-8 编码

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原文出处, 即下面的声明.

原文出处:http://blog.chenlb.com/2010/01/python-use-utf-8.html
一般我喜欢用 utf-8 编码,在 python 怎么使用呢?
1、在 python 源码文件中用 utf-8 文字。一般会报错,如下:
File "F:\workspace\psh\src\test.py", line 2
SyntaxError: Non-ASCII character '\xe4' in file F:\workspace\psh\src\test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

test.py 的内容:
print "你好";

如果要正常运行在 test.py 文件前面加编码注释,如:
#!/usr/bin/python2.6   
# -*- coding: utf-8 -*-   
print "你好"

2、
python 对 url encode UTF-8 怎么做呢?
windows 的命令行参数转 utf-8 怎么做呢?
代码:
# -*- coding: utf-8 -*-   
import urllib   
import sys   
if __name__ == '__main__':   
if len(sys.argv) > 1:   
str = sys.argv   
str = unicode(str, 'gbk')   
else:   
str = "中文"
print str   
params = {}   
params['name'] = str.encode("UTF-8")   
print urllib.urlencode(params)

python 内部是用 unicode 吧。
由于 windows 的命令行输入的是 GBK 编码的,可以要先转为 unicode(第三8行)。
要转 url encode 时,先把 str 转为 utf-8。
默认的输出结果:
中文
name=%E4%B8%AD%E6%96%87
写 python 脚本来做写小事情方便,比如要取些 solr 的数据,solr 的 url 编码是 utf-8 的。
参考:http://evanjones.ca/python-utf8.html
页: [1]
查看完整版本: Python 使用 UTF-8 编码