ÔÌû£º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??Êdz¬³ö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() ......
дÁ˼¸¸öÓйØoperaminimodµÄpythonС³ÌÐò
firefox->opmÊéǩת»»
import re
def pipeiwangzhi(a):
s=[]
pp= re.compile(r'<DT><A HREF="(.*)" ADD_DATE=(.*>)(.*)</A>')
m=pp.search(a)
s1=[]
if m!=None:
s=m.groups()
s1.append(s[0])
s1.append(s[2])
return s1
fnlang='f:\\bookmarks.html'
f1 = open(fnlang,'rb')
ffbk=f1.readlines()
f1.close()
opbkmg=[]
for i in ffbk:
a=i.decode('utf-8')
ss=[]
ss=pipeiwangzhi(a)
if ss!=[]:
opbkmg.append(ss)
#print(opbkmg)
tou="""<!DOCTYPE NETSCAPE-Bookmark-file-1>
&l ......
ÕâÊÇÒ»¸ö¼òµ¥µÄ±í´ïʽ¼ÆËãÆ÷, ²»Ì«¶®ÓÃTkinterдGUI, ²Î¿¼Á˱ðÈ˵ĴúÂë
from __future__ import division
from Tkinter import Tk, Entry, Button, Label, mainloop
from tkFont import Font
def get_value ():
v = ''
try:
v = eval(text.get()) #use eval to calculate the vlaue of the text
except:
pass
if isinstance(v, (int, float, long)):
pass
else:
v = 'Error...'
label.config(text = v) #use config to change the text
top = Tk()
top.title("calculator")
ft = Font(family = 'Courier New', size = 12)
text = Entry(top, font = ft)
button = Button(top, text = 'Ok', command = get_value)
label = Label(text = '(+ - * / % **)', font = ft)
Enter = lambda x: x.keycode == 13 and get_value()
Key = lambda x: label.config(text = '(+ - * / % **)')
text.bind('<Key>', Enter) #when the key is enter, execute the function Enter
text.focus()
text.bind('<Button-1>', Key) #when click the left-key, execute the function Key
text.pack()
button.pack()
label.pack()
mainloo ......
ÕâÊÇÒ»¸öÎÒÃÇÔÚ´¦ÀíÖÐÎÄʱ, ¾³£Óöµ½µÄÎÊÌâ.
pythonÀïÃæ»ù±¾ÉÏÒª¿¼ÂÇÈýÖÖ±àÂë¸ñʽ
1 Ô´Îļþ±àÂë
ÔÚÎļþÍ·²¿Ê¹ÓÃcodingÉùÃ÷¡£¸æËßpython½âÊÍÆ÷¸Ã´úÂëÎļþËùʹÓõÄ×Ö·û¼¯¡£
#/usr/bin/python
#coding: utf8
2 ÄÚ²¿±àÂë
´úÂëÎļþÖеÄ×Ö·û´®£¬¾¹ýdecodeÒԺ󣬱»×ª»»ÎªÍ³Ò»µÄunicode¸ñʽµÄÄÚ²¿Êý¾Ý£¬ÀàËÆÓÚu'*'¡£unicodeÊý¾Ý¿ÉÒÔʹÓÃencodeº¯Êý£¬ÔÙ×ÔÓÉת»»ÎªÆäËû¸ñʽµÄÊý¾Ý£¬Ï൱ÓÚÒ»¸öͳһµÄƽ̨¡£
Ö±½ÓÊäÈëunicodeÊý¾Ý
>>> u'ÄãºÃ'
u'\u4f60\u597d'
½«unicodeÊý¾Ýת»»Îªgb2312¸ñʽ
>>> u'ÄãºÃ'.encode('gb2312')
'\xc4\xe3\xba\xc3'
½«ÊäÈëµÄgb2312¸ñʽµÄÊý¾Ý½âÂëΪunicode
>>> 'ÄãºÃ'.decode('gb2312')
u'\u4f60\u597d'
ÊäÈëÊý¾ÝµÄ¸ñʽȡ¾öÓÚËùÓÃshellÖն˵ıàÂëÉèÖ㬱¾ÀýÖÐΪzh_CN
[root@chenzheng python]# echo $LANG
zh_CN
½âÂëͬʱת»»Îªutf8
>>> 'ÄãºÃ'.decode('gb2312').encode('utf8')
'\xe4\xbd\xa0\xe5\xa5\xbd'
3 ÍⲿÊäÈëµÄ±àÂë
ÆäʵÕâ¸öºÍÔÚpython½»»¥shellÖÐÊäÈëµÄ×Ö·û´®£¬ËùÓöµ½µÄÇé¿ö»ù±¾Ò»Ñù¡£µ«³ÌÐòÖг£³£Óõ½´ÓÍøÂ磬Îļþ¶ÁÈ¡µÄÊý¾Ý£¬¹Ê´Ëµ¥¶ÀÁгö£¬ÐèÒªÌØ±ð×¢ÒâÆä±àÂë¸ñʽÊÇ·ñÓÚÏµÍ³ÒªÇ ......
1.³£Ó÷½·¨£¬²»´ø²ÎÊý
def decator(func):
def inner_func(*args):
args = (i * 2 for i in args)
return func(*args)
return inner_func
@decator
def add(a, b, c):
return a + b + c
2.´ø²ÎÊý£º
class Dec(object):
def __init__(self, i):
self.i = i
def __call__(self, func):
def inner_func(a, b, c):
return self.i * func(a, b, c)
return inner_func
@Dec(2)
def abc(x, y, z):
print "function abc"
print (x, y, z)
return x + y + z ......
1.³£Ó÷½·¨£¬²»´ø²ÎÊý
def decator(func):
def inner_func(*args):
args = (i * 2 for i in args)
return func(*args)
return inner_func
@decator
def add(a, b, c):
return a + b + c
2.´ø²ÎÊý£º
class Dec(object):
def __init__(self, i):
self.i = i
def __call__(self, func):
def inner_func(a, b, c):
return self.i * func(a, b, c)
return inner_func
@Dec(2)
def abc(x, y, z):
print "function abc"
print (x, y, z)
return x + y + z ......