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 的內容:
如果要正常運行在 test.py 文件前面加編碼注釋,如:
2、
python 對 url encode UTF-8 怎么做呢?
windows 的命令行參數轉 utf-8 怎么做呢?
代碼:
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
轉載于:https://www.cnblogs.com/qq78292959/archive/2013/03/22/2974926.html
總結
以上是生活随笔為你收集整理的Python 使用 UTF-8 编码(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决安装python第三方模块 '-mn
- 下一篇: 运行管理员线程和用户线程小练习