python使用 UTF-8编码

来源:互联网 发布:河北省网络行政学院 编辑:程序博客网 时间:2024/06/05 08:02

Python 使用 UTF-8 编码

发表于:2010年1月28日 | 分类:Python | 标签: utf8 | views(58,751)

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

原文出处: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 的内容:

  1. print "你好"  

如果要正常运行在 test.py 文件前面加编码注释,如:

  1. #!/usr/bin/python2.6  
  2. # -*- coding: utf-8 -*-  
  3. print "你好"  

2、
python 对 url encode UTF-8 怎么做呢?

windows 的命令行参数转 utf-8 怎么做呢?

代码:

  1. # -*- coding: utf-8 -*-  
  2. import urllib  
  3. import sys  
  4.   
  5. if __name__ == '__main__':  
  6.     if len(sys.argv) > 1:  
  7.         str = sys.argv[1]  
  8.         str = unicode(str, 'gbk')  
  9.     else:  
  10.         str = "中文"  
  11.   
  12.     print str  
  13.     params = {}  
  14.     params['name'] = str.encode("UTF-8")  
  15.   
  16.     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

原创粉丝点击