用惯了C系语言,用python还是有点不习惯,因为python是动态语言,不需要声明变量,在赋值时是什么类型,就是什么类型,想看变量类型,用好print和type()命令就好了,在理解动态语言和静态语言,强类型和弱类型的过程中,碰到一篇不错的科普文[语言类型]。(http://www.cnblogs.com/yaoxiaodan/p/5285735.html)
在使用过程中,python的许多命令感觉和Linux下的shell十分相似,嵌了很多轮子可以直接拿来用,想要看一个函数的用法或者模块的使用,只要用好help(’ ‘)即可。
decode和encode则能解决大部分python中出现的乱码问题,python使用的是Unicode编码,
-
decode 的作用是将其他编码的字符串转换成 Unicode 编码,eg name.decode(“GB2312”),表示将GB2312编码的字符串name转换成Unicode编码
表示这个对象按照对象解码,在python环境下显示输出就正常了。 -
encode 的作用是将Unicode编码转换成其他编码的字符串,eg name.encode(”GB2312“),表示将GB2312编码的字符串name转换成GB2312编码
name对象就像被加密了,这时候显示输出就是乱码。
而开头的注释是指python源文件中存在非ASCII码字符,则需在头部进行编码声明,编码问题详解
python 只检查 #、coding 和编码字符串,所以在编码声明语句出现其他字符也没有关系,其编码说明的正则表达式为:
\%^.*\n.∗\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$
- 1
关于正则表达式Re通常用作字符串匹配,python因此也经常拿来写写爬虫,给自己挖个坑,有时间再详述。
没有编码说明,而又有非ASCII码,就会出现这样的情况:
一个月的开发实习,基于web.py框架,主要是跟数据库打交道,提交表单,更新表单,增删查改
一个小练习的后台代码:
#encoding:utf8
import web
def count_tax(arg) :
argc=arg-3500;
if argc > 0:
if argc>80000:
tax = (argc-80000)*0.45 + 25000*0.35 + 20000*0.3 + 26000*0.25 + 4500*0.2 + 3000*0.1 + 1500*0.03;
elif argc > 55000:
tax = (argc-55000)*0.35 + 20000*0.3 + 26000*0.25 + 4500*0.2 + 3000*0.1 + 1500*0.03;
elif argc > 35000:
tax = (argc-35000)*0.3 + 26000*0.25 + 4500*0.2 + 3000*0.1 + 1500*0.03;
elif argc > 9000:
tax = (argc-9000)*0.25 + 4500*0.2 + 3000*0.1 + 1500*0.03;
elif argc > 4500:
tax = (argc-4500)*0.2 + 3000*0.1 + 1500*0.03;
elif argc > 1500:
tax = (argc-1500)*0.1 + 1500*0.03;
else:
tax=argc*0.03;
else:
tax=0;
print tax;
return tax;
db = web.database(dbn="mysql",user="root",pw="987654321",db="test",charset='utf8')
urls = (
'/', 'index',
'/add','add',
'/delete','delete',
'/view/(.*)', 'view',
'/change','change',
'/deleteSelect','deleteSelect',
'/changePage','changePage',
'/search','search'
)
render = web.template.render('template/')
class view:
def GET(self,uri):
i = web.input()
for key,val in i.iteritems():
id = int(val)
update_user = db.select('user', where="id=$id",vars=locals() )
return render.update(update_user)
class index:
def GET(self):
staff = db.select('user',offset =0, limit = 5)
sum = db.query("select count(*)from user")
count = sum[0]['count(*)']
print count
currentPage = 1
return render.index(staff,currentPage,count)
class add:
"""docstring for add"""
def POST(self):
i = web.input()
print i
if(i.salary == ''):
n = db.insert('user',name = i.name)
else:
n = db.insert('user',name = i.name,salary = i.salary,tax = count_tax(int(i.salary)))
raise web.seeother('/')
class delete:
def POST(self):
params = web.input()
g = int(params['deleteId'])
# print type(g)
# web.debug('*'*20, locals())
#where is special ,vars is useful to where
db.delete('user', where="id=$g",vars = locals() )
raise web.seeother('/')
class change:
def POST(self):
i = web.input()
print i
oldId = i['oldId']
updName = i['name']
db.update('user',where="id=$oldId",name=updName,salary=i['salary'],tax=count_tax(int(i['salary'])), vars = locals())
raise web.seeother('/')
class deleteSelect:
def POST(self):
#之前若不指定默认参数,则,是一个字典,构造的时候根据键唯一,后面的覆盖
i=web.input(checkbox=[])
for n in i['checkbox']:
n = int(n)
db.delete('user', where="id=$n",vars = locals() )
raise web.seeother('/')
class changePage:
def POST(self):
i = web.input()
currentPage = int(i['inputForpage'])
print currentPage
devi = (currentPage-1)*5
staff = db.select('user',offset = devi, limit = 5)
#重新统计个数是有必要的,因为数据库可能发生变动
sum = db.query("select count(*)from user")
count = sum[0]['count(*)']
print count
print currentPage
return render.index(staff,currentPage,count)
class search(object):
"""docstring for search"""
def POST(self):
i = web.input()
content = i['searchContent']
print content
records = db.query("select * from user where name LIKE $content or salary LIKE $content or tax LIKE $content ",vars={'content':'%' + content + '%'})
print records
sum = db.query("select count(*)from user")
count = sum[0]['count(*)']
return render.index(records,1,count)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127