Python 使用 UTF-8 编码(转)

Python 使用 UTF-8 编码(转)

原文出处:http://blog.chenlb.com/2010/01/python-use-utf-8.html

一般我喜欢用 utf-8 编码,在 python 怎么使用呢?

1、在 python 源码文件中用 utf-8 文字。一般会报错,如下:

File "F:workspacepshsrc est.py", line 2 SyntaxError: Non-ASCII character 'xe4' in file F:workspacepshsrc est.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 的。

原文地址:https://www.cnblogs.com/blueicely/p/3445167.html