以及循环递归..封装及笔记
文件操作 文件操作 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 import osos.chdir('../p' ) os.getcwd(); data = open('sketch.txt' ) data.seek(0 ); each_line = data.readline(); for each_line in data: if each_line.find(':' ) != -1 : try : (role,line_spoken) = each_line.split(':' ,1 ) print(role,end = '' ); print(' said: ' ,end = '' ); print(line_spoken,end = '' ); except : pass out = open("data.out" ,"w" ) for eachS in other: print(eachS,file = out) out.close();
##python2(HBK)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import codecsf=codecs.open('file_ch.txt' ,'w' ,'utf-8' ) f.write(u'用python做些事\n' ) f.write(u'黑板客\n' ) f.write(u'网易云课堂\n' ) f.close() f=codecs.open('file_ch.txt' ,'r' ,'utf-8' ) print f.readline() print f.readline()print f.readline()f.close() import osprint os.path.exists('file_ch.txt' )os.rename('file_ch.txt' , 'file_test.txt' ) print os.path.exists('file_ch.txt' )
shelve & pickle 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 import shelve f = shelve.open('file_test.shelve' ) f['baidu' ] = 'www.baidu.com' f['qq' ] = 'www.qq.com' f['163' ] = 'www.163.com' print ff.close() g = shelve.open('file_test.shelve' ) print gprint "--------------------------------------" import cPicklef=open('file_test.pkl' ,'w' ) obj1 = 2015 ,"heibanke" ,[1 ,2 ,3 ,4 ],{"python" :1990 ,"java" :1992 } obj2 = ['heibanke' ,'junmin11' ,'chutianshu1981' ,'sjtujoe' ,'ygkkv' , 'liuyanping_0904' ,'zhkmxx930' ]cPickle.dump(obj1,f) cPickle.dump(obj2,f) f.close() f=open('file_test.pkl' ,'r' ) obj1_r = cPickle.load(f) print obj1_robj2_r = cPickle.load(f) print obj2_rf.close()
Others HEADFIRST PYTHON 循环与递归
python默认递归深度100
参考代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 """ 递归打印多重列表 """ movies = ["The Holy Grail" , "The Life of Brian" , "The Meaning of Life" , [1 ,2 ,3 ]]; def pt (List) : for a in List: if isinstance(a,list): pt(a); else : print(a); pt(movies);
封装以及命名空间
nester.py
放到一个文件夹中,加上setup.py
1 2 3 4 5 6 7 8 9 10 11 from distutils.core import setupsetup( name = 'nester' , version = '1.0.0' , py_modules = ['nester' ], author = 'abc' , author_email = '505636638@qq.com' , url = "baidu.com" , discription = '1' , )
在文件夹中执行
1 2 python3 setup.py sdist; sudo python3 setup.py install
import
后就可以通过命名空间执行了
1 2 3 4 import nesternester.pt(nester.movies)
数据处理 1 2 line_spoken = line_spoken.strip() locals()
第一本编程书笔记
int(), str()对于类型的转化
[a:b]对于数组的分片
函数传入参数可以(id = 1,)不按顺序传入
print中sep表示分隔字符
**
为幂,//
为整除
列表推倒:
1 2 k = [n for n in range(1 ,10 ) if n % 2 == 0 ] g = {i:j.upper() for (i,j) in zip(range(1 ,6 ),'abcde' )}
类的继承 在括号内写父类
exercise 1 2 3 4 5 6 7 8 9 10 11 12 13 def creater (name) : name = name + '.txt' file = open('/users/macbook/desktop/' + name,'w' ) file.write('hello' + name) return file for x in range(1 ,11 ): if (x < 10 ): creater('0' + str(x)) else : creater(str(x)) pass
<
python实战计划 制作自己网页
Greedy & Violent
>