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()
相关文档:
Python 社区有句俗语:“Python 自己带着电池。” 别自己写计时框架。Python 2.3以后 、具备一个叫做 timeit 的完美计时工具。DiveInto中的例子
>>> import timeit
>>> t = timeit.Timer("soundex.soundex('Pilgrim')",
... "import soundex")
>>> t.timeit() ......
真是倒霉,刚买不久的移动硬盘,昨天删除一个分区失败后,几个分区都不见了,拿去修,未果
换了个新的,但其中数据全没了。那是我平时收集的很有用的资料
很多都可以重新下载,但怎能想起硬盘中的所有东西
今天换硬盘回来
就像写一个保存指定路径下所有文件夹和文件名的程序
这样,如果东西丢了,看看那里有些什么,也 ......
Python的os模块,包含了普遍的操作系统功能,这里主要学习与路径相关的函数:
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回当前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
......
8.8. queue — A synchronized queue class¶
queue -- 一个同步队列类
The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implemen ......