python个人笔记,纯属方便查询
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
|
中间退出的时候要保存,然后再次进入的时候还是退出的那个点。
字典
-
-
-
-
-
字符串(硬盘)
-
-
-
-
-
-
字典
pickle语法dump:把字典写入硬盘文件中:
import
pickle
account_info
=
{
'a'
:
'bbbbbbbbb'
,
'b'
:
'ccccccccc'
}
f
=
file
(
'account.pki'
,
'wb'
)
#定义一个二进制文件,只有字符串才能写入硬盘,
#所以字典要先转换为字符串。
pickle.dump(account_info,f)
f.close()
从硬盘中读取文件变成字典:
import
pickle
pki_file
=
open
(
'account.pki'
,
'rb'
)
#以读的方式打开一个文件。
account_list
=
pickle.load(pki_file)
#把文件序列化,以原来的格式读取
pki_file.close()
如果不写到文件,而是通过socket发送消息:
a
=
{
'a'
:
'bbbbbbbbb'
,
'b'
:
'ccccccccc'
}
b
=
pickle.dumps(a)
c
=
pickle.loads(b)
pickle正则表达式:
import
re
p
=
re.
compile
(
'hello'
)
#匹配hello
str_a
=
'hello,my nam is darren'
p.match(str_a)
m
=
p.match(str_a)
m.group()
#看返回值,如果是none则没有匹配
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
p
=
re.
compile
(
'my'
)
p.serach(str_a)
#匹配整行
if
m
is
not
none:
print
"mactched"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
m
=
re.search(
'my'
,str_a)
m.group()
re.search(
'my'
,str_a).group()
#一行搞定
模糊查询:
print
re.findall(
'\s+'
, str_a)
#\s+匹配空格的
print
re.findall(
'\S+'
, str_a)
#\S+匹配去掉空格的
print
re.findall(
'\d+'
, str_a)
#\d+匹配数字的
print
re.findall(
'\D+'
, str_a)
#\D+匹配去掉数字的
print
re.split(
'\d+'
, str_a)
#匹配到的做为分隔符
print
re.split(
'b'
, str_a)
#以b做为分隔符。
print
re.split(
'\\\\'
, str_a)
#前面两个为转义,后面两个以\\为分隔符
print
re.split(re.escape(
'\\'
), str_a)
#和上面一样
re.sub(
'\d+'
,
'|'
,a)
#把a中的空格替换为|。
re.sub(
'\d+'
,
'|'
,a,
1
)
#把a中的空格替换为|,只匹配一个
.
#匹配任意一个字符
.
+
#匹配任意一个或多个字符
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
python模块
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
如果导入一个目录:例如day3,这时就需要在day3目录下建一个空文件。
cd day3
touch __init__.py
导入一个交互的配置文件,需要先把交换的脚本改成一个函数:
def
sayhi ():
if
dayhi
=
=
main()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
python
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
装饰器:
查看sayhi()执行花费的时间:
import
time
def
sayhi():
start
=
time.time()
print
'hi,your sister.....'
time.sleep(
0.5
)
end
=
time.time()
print
'this function costs :'
,end
-
start
sayhi()
如果计算一百个函数花费的时间:
import
time
def
time_counter(func):
def
wrapper():
start
=
time.time()
func()
end
=
time.time()
print
'this function costs :'
,end
-
start
return
wrapper
@time_counter
def
sayhi():
print
'hi,your sister.....'
@time_counter
def
salary():
print
'your salary:'
sayhi()
salary()
|
本文转自 王家东哥 51CTO博客,原文链接:http://blog.51cto.com/xiaodongge/1877785