python中编码转换
当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
*在日本系统中,可以通过设置成utf-8或者 cp932实现对日文文字,中文文字的正常显示。
2. 测试已经成功的方法:
>>> import sys
>>> sys.getdefaultencoding()
相关文档:
由于工作需要,又要学习新的开发语言-Python, 语言学过几种了, 感觉还是有好多相似的地方, 这个Python感觉就跟Java有好多相通的地方, 首先你可以在Eclipse上通过配置后来开发Python。这里我听从了老大的建议,配置了一个FlexBuilder 作为Python的IDE进行开发。
目前刚开始学习Python的基本语法, 今天主要看了一下Pyth ......
转自:
http://hi.baidu.com/feng2211/blog/item/8b86b6d9816a3f2710df9b79.html
和
http://i.19830102.com/archives/164
Python 版本:2.6
下载地址:http://www.python.org/download/releases/2.6.1/
下载msi文件并安装
MySQLdb版本: MySQL-python-1.2.2.win32-py2.6.exe
下载地址:http://home.netimperia.com/ ......
#将一些类型的文件压缩为7z.py
#for folder all file do 7z
import os
import sys
import distutils.file_util
def ImportOkFile():
if(os.path.isfile("D:\\Records\\将一些类型的文件压缩为7z_record.txt")==False):
f=open("D:\\Reco ......
Python内建异常体系结构
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
| +-- BufferError
| +-- ArithmeticError
| | +-- FloatingPointError
| | +-- OverflowError
| ......