python file

以及循环递归..封装及笔记

文件操作

文件操作

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 os
os.chdir('../p')
os.getcwd();#get file address

#I
data = open('sketch.txt')
data.seek(0);#return begin

each_line = data.readline();
# help(each_line.split)
# S.split(sep=None, maxsplit=-1) -> list of strings

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
#O
out = open("data.out","w") #a -> append,w+ read & write
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
#!/usr/bin/env python
# coding: utf-8
#copyRight by heibanke

########################## file ################
import codecs
f=codecs.open('file_ch.txt','w','utf-8') # 支持中文的打开

f.write(u'用python做些事\n')
f.write(u'黑板客\n')
f.write(u'网易云课堂\n')
f.close()

#read file
f=codecs.open('file_ch.txt','r','utf-8')
print f.readline() #read()是读取一个字符
print f.readline()
print f.readline()
f.close()
########################## os ################
import os
print 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
#!/usr/bin/env python
# coding: utf-8
#copyRight by heibanke

########################## shelve ################
import shelve # 类似于字典,但是存在本地...
f = shelve.open('file_test.shelve')
f['baidu'] = 'www.baidu.com'
f['qq'] = 'www.qq.com'
f['163'] = 'www.163.com'

print f
f.close()
g = shelve.open('file_test.shelve')
print g


########################## pickle ################
print "--------------------------------------"
import cPickle
f=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()

#read file
f=open('file_test.pkl','r')
obj1_r = cPickle.load(f)
print obj1_r

obj2_r = cPickle.load(f)
print obj2_r
f.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);
    # for a in range(0,len(List)):
    # if isinstance(List[a],list):
    # pt(List[a]);
    # else:
    # print(List[a]);
    pt(movies);

封装以及命名空间

  1. nester.py放到一个文件夹中,加上setup.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    from distutils.core import setup

    setup(
    name = 'nester',
    version = '1.0.0',
    py_modules = ['nester'],
    author = 'abc',
    author_email = '505636638@qq.com',#信息基本都是瞎写的= =
    url = "baidu.com",
    discription = '1',
    )
  2. 在文件夹中执行

    1
    2
    python3 setup.py sdist;
    sudo python3 setup.py install
  3. import后就可以通过命名空间执行了

    1
    2
    3
    4
    import nester
    nester.pt(nester.movies)
    #from nester import pt <=> using namespace...
    #pt(nester.movies)

数据处理

1
2
line_spoken = line_spoken.strip()#去除不必要的空白
locals()#当前BIF中变量集合

第一本编程书笔记

  • 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
#创造1-10.txt文本,以数字命名
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
文章目录
  1. 1. 文件操作
    1. 1.1. 文件操作
    2. 1.2. shelve & pickle
  2. 2. Others
    1. 2.1. HEADFIRST PYTHON
    2. 2.2. 循环与递归
    3. 2.3. 封装以及命名空间
      1. 2.3.1. 数据处理
    4. 2.4. 第一本编程书笔记
      1. 2.4.1. exercise