PYTHON设置默认语言编码
原帖:http://www.cnblogs.com/jingleguo/archive/2008/06/02/1211820.html
当python中间处理非ASCII编码时,经常会出现如下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
0x??是超出128的数字,python在默认的情况下认为语言的编码是ascii编码,所以无法处理其他编码,需要设置python的默认编码为所需要的编码。
一个解决的方案是在代码中添加:
import sys
reload(sys)
sys.setdefaultencoding('gb2312')
另一个方案是在python的Lib\site-packages文件夹下新建一个sitecustomize.py
文件(sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically.),输入:
import sys
sys.setdefaultencoding('gb2312')
这样就能够自动的设置编码了。
ps:
1. utf8的编码是:utf-8
2. 测试已经成功的方法:
>>> import sys
>>> sys.getdefaultencoding()
相关文档:
* java强制使用OO进行应用开发,Python可以用函数式编程。函数式有时显得非常简单,代码量巨减。回想起Java动不动的N层、还有一个个对象,真的有那种心要吗?而且Python本身语言风格就简洁,再加上简洁的函数,在开发效率、Debug、测试上似乎会有优势。
* 大型复杂项目的性能更多地体现在架构、硬件拓扑结构上等。应对 ......
Python代码
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
......
闲来无事, 玩玩python...
是采用有道翻译, 然后抓取网页的.
import re, urllib
url="http://dict.youdao.com/search?le=eng&q="
print ("input q to exit")
while 1:
word = raw_input(">>>")
if word=="q":
exit()
else:
word = word.replace(' ', '+')
url += word
u ......
16.1. select — Waiting for I/O completion¶
This module provides access to the select and poll functions available in most operating systems, epoll available on Linux 2.5+ and kqueue available on most BSD. Note that on Windows, it only works for sockets; on other operating systems, it al ......