-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_note_csvt
More file actions
1171 lines (822 loc) · 20 KB
/
python_note_csvt
File metadata and controls
1171 lines (822 loc) · 20 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
http://study.163.com/course/introduction.htm?courseId=378003#/courseDetail?tab=1
标准数据类型
46Python3 中有六个标准的数据类型:
47Number(数字)
48String(字符串)
49List(列表)
50Tuple(元组)
51Sets(集合)
52Dictionary(字典)
53Number(数字)
54Python3 支持 int、float、bool、complex(复数)。
55在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。
http://www.runoob.com/python3/python3-data-type.html
##copy line
Ctrl-a 光标到行首
Ctrl-Shift-Space 设置标记
Ctrl-e 光标到行尾。如此这一行就被选为激活的区域了
Alt-w 复制当前激活的区域
选中某一块区域:CTRL+@(CTRL+SHIFT+2)或者CTRL+SPACE 移动光标到合适的位置(CTRL+p、CTRL+n、CTRL+f、CTRL+b分别是向上、下、前、后 来移动光标)。
剪切快捷键:CTRL+k 删除当前位置到行末,CTRL+d相当于键盘上的DELETE键,
删除前面的字符:BACKSPACE
复制快捷键:ALT+w,将当前选中的文本复制到缓冲区。
粘贴快捷键:CTRL+y
保存快捷键:CTRL+x 然后按 CTRL+s
退出快捷键:CTRL+x 然后: CTRL+c
打开文件: CTRL+x 然后 : CTRL+f
http://www.icoolxue.com/album/show/113
Video DownloadHelper
C:\Users\Administrator\dwhelper
== 01.走进Python==
pass
== 04 pass==
pass
== 05.pass数据类型(数字和字符串) ==
str
In [1]: a='abcde'
In [2]: a[0]
Out[2]: 'a'
拼接字符串
In [3]: a[0]+a[1]
Out[3]: 'ab'0
In [5]: a[::1]
Out[5]: 'abcde'
In [6]: a[-4:-1]
Out[6]: 'bcd'
input and raw_input (会作合法检查)
str , repr 和反引号 是将python值转换为字符串的3种方法. function str让字符串更容易阅读,而repr(和反引号)则把结果字符串转换为合法的python表达式
== 06.元组==
pass
== 07.列表==
pass
一
[] 可作为列表,也可以是切片
Help on built-in function append:
help(list.append)
append(...)
L.append(object) -- append object to end
(END)
== 08.字典 ==
pass
== 09.流程控制(if语句==
pass
if 1<2:
print "ok"
if else 之间不能用其实内容 空行可以
== 10.流程控制(逻辑) ==
pass
In [3]: True and True
Out[3]: True
In [4]: True and False
Out[4]: False
In [5]: 1 and 1 and 1
Out[5]: 1
== 11流程控制(for语句) ==
pass
range xrange
In [17]: num = 0
In [18]: for x in range(1,101):
...: num += x
...:
In [19]: print num
5050
== 12.流程控制(遍历序列及字典) ==
pass
遍历字典
evan@evankali:~/github/python/test$ cat 2.py
#!/usr/bin/python
s = "hello"
l = [1,2,3,'a','b']
t = (7,8,9,'x','y')
d = {1:111,2:222,5:555,3:333}
for x in l:
if x >=2:
print x
for x in range(len(s)):
print s[x]
evan@evankali:~/github/python/test$ cat di.py
#!/usr/bin/python
s = "hello"
l = [1,2,3,'a','b']
t = (7,8,9,'x','y')
d = {1:111,2:222,5:555,3:333}
for x in d:
print d[x]
evan@evankali:~/github/python/test$ cat di.py
#!/usr/bin/python
s = "hello"
l = [1,2,3,'a','b']
t = (7,8,9,'x','y')
d = {1:111,2:222,5:555,3:333}
#for x in d:
# print d[x]
for k,v in d.items():
print k
print v
evan@evankali:~/github/python/test$ python di.py
1
111
2
222
3
333
5
555
== 13.流程控制(循环控制) ==
pass
evan@evankali:~/github/python/test$ cat di.py
#!/usr/bin/python
s = "hello"
l = [1,2,3,'a','b']
t = (7,8,9,'x','y')
d = {1:111,2:222,5:555,3:333}
for x in d:
print d[x]
else:
print "1ending"
for k,v in d.items():
print k
print v
else:
print "en"
evan@evankali:~/github/python/test$ python di.py
111
222
333
555
1ending
1
111
2
222
3
333
5
555
en
#!/usr/bin/python
import time
s = "hello"
l = [1,2,3,'a','b']
t = (7,8,9,'x','y')
d = {1:111,2:222,5:555,3:333}
for x in range(3):
print x
time.sleep(1)
else:
print "1ending"
evan@evanpc:~/github/python/test$ python 13.py
0
1
2
1ending
evan@evanpc:~/github/python/test$ cat 13.py
#!/usr/bin/python
import time
for x in range(1,11):
print x
if x == 6:
break
else:
print "1ending"
evan@evanpc:~/github/python/test$ python 13.py
1
2
3
4
5
6
==14.流程控制(while循环) ==
06:00
evan@evankali:~/github/python/test$ python while14.py
hey
ple input somethin, q for quit:q
hey
ple input somethin, q for quit:^CTraceback (most recent call last):
File "while14.py", line 5, in <module>
x =raw_input("ple input somethin, q for quit:")
KeyboardInterrupt
evan@evankali:~/github/python/test$ cat while14.py
#!/usr/bin/python
while True:
print "hey"
x =raw_input("ple input somethin, q for quit:")
evan@evankali:~/github/python/test$ python while14.py
hey
ple input somethin, q for quit:q
evan@evankali:~/github/python/test$ cat while14.py
#!/usr/bin/python
while True:
print "hey"
x =raw_input("ple input somethin, q for quit:")
if x == "q":
break
evan@evankali:~/github/python/test$ cat while14_1.py
#!/usr/bin/python
x= ""
while True:
print "hey"
x =raw_input("ple input somethin, q for quit:")
if x == "q":
break
evan@evankali:~/github/python/test$ python while14_1.py
hey
ple input somethin, q for quit:a
hey
ple input somethin, q for quit:q
#改良 回车自动退出
evan@evankali:~/github/python/test$ cat while14_1.py
#!/usr/bin/python
x= ""
while x != "q":
print "hey"
x =raw_input("ple input somethin, q for quit:")
if not x :
break
evan@evankali:~/github/python/test$ python while14_1.py
hey
ple input somethin, q for quit:a
hey
ple input somethin, q for quit:
#!/usr/bin/python
x= ""
while x != "q":
print "hey"
x =raw_input("ple input somethin, q for quit:")
if not x :
break
else:
print "enddo"
$ python while14_1.py
hey
ple input somethin, q for quit:hey
hey
ple input somethin, q for quit:q
enddo
emacs while14_1.py
evan@evankali:~/github/python/test$
evan@evankali:~/github/python/test$ cat while14_1.py
#!/usr/bin/python
x= ""
while x != "q":
print "hey"
x =raw_input("ple input somethin, q for quit:")
if not x :
break
if x == "c":
continue
print "one more time"
else:
print "enddo"
evan@evankali:~/github/python/test$ python while14_1.py
hey
ple input somethin, q for quit:love
one more time
hey
ple input somethin, q for quit:c
hey
ple input somethin, q for quit:
== 15.函数(定义和调用) ==
pass
In [1]: def add():
...: c = a+b
...: print c
...:
In [3]: a = 100
In [4]: b = 200
In [5]: add()
300
#!/usr/bin/python
a = 100
def fun():
if True:
print "good"
print a
fun()
== 16.函数(形参实参默认参数)==
****
evan@evankali:~/github/python/test/16$ python 1.py
input some:x
x
evan@evankali:~/github/python/test/16$ cat 1.py
def fun(x):
# if True:
print x
s=raw_input("input some:")
fun(s)
*****
evan@evanpc:~/github/python/test/16$ cat 1.py
def fun(x,y):
if x == y :
print x,'=' , y
else:
print x,'!=' ,
s1 = raw_input("input some:")
s2 = raw_input("input some:")
fun(s1,s2)
evan@evanpc:~/github/python/test/16$ python 1.py
input some:hey
input some:hey
hey = hey
evan@evanpc:~/github/python/test/16$ python 1.py
input some:hey
input some:he
hey !=
******************
evan@evanpc:~/github/python/test/16$ cat 2.py
#!/usr/bin/python
#coding:utf8
def mashine(x=3,y='奶油'):
print "生成一个",x,'元',y,'口味的冰'
#mashine(5,'巧克力')
mashine(5,)
#mashine(x=5)
evan@evanpc:~/github/python/test/16$ python 2.py
生成一个 5 元 奶油 口味的冰
***
设置默认参考是自右向左
def mashine(x=3,y):
不行
而
def mashine(x,y=3):
可以
总结 就是右边的是一定有值的
==17.函数(变量作用域)==
evan@evanpc:~/github/python/test/17$ cat fu.py
#!/usr/bin/python
x = 'I am global var'
def fun():
x = 100
global y
y = 200
print x
fun()
print x
print y
evan@evanpc:~/github/python/test/17$ python fu.py
100
I am global var
200
evan@evanpc:~/github/python/test/17$ cat fu.py
#!/usr/bin/python
x = 'I am global var'
def fun():
x = 100
global y
y = 200
print x
print y
# fun 没有运行 所以报错
evan@evanpc:~/github/python/test/17$ python fu.py
Traceback (most recent call last):
File "fu.py", line 11, in <module>
print y
NameError: global name 'y' is not defined
evan@evanpc:~/github/python/test/17$ cat fu.py
#!/usr/bin/python
x = 'I am global var'
def fun():
global y
y = 200
global x
x = 100
fun()
print x
evan@evanpc:~/github/python/test/17$ python fu.py
100
== 18.函数及return返回值==
11:00
In [1]: def f(x,y):
...: print "welcome"
...: return x+y
...:
In [2]: f(2,3)
welcome
Out[2]: 5
but 正常情况其实
def f(x,y):
print "welcome"
return x+y
f(2,3)
evan@evanpc:~/github/python/test/17$ python f.py
welcome
return 执行后 function 就结束了
In [1]: def f():
...: return "one"
...: return "two"
...:
In [2]: f()
Out[2]: 'one'
evan@evanpc:~/github/python/test/17$ cat x.py
def f(x,y):
if x>y:
return 1
if x <y:
return -1
return 0
print f(1,2)
evan@evanpc:~/github/python/test/17$ python x.py
-1
== 19.函数冗余参数==
元组 字典什么 的可以作实参
多用return 少用print 除非你是想要打印出来
In [1]: def f(x,y):
...: print x,y
...:
In [2]: f(1,2)
1 2
In [3]: t=('a','b')
In [4]: f(t)
-----------------------------------------------------------------
TypeError
In [5]: type(t)
Out[5]: tuple
In [6]: f(t,'te')
('a', 'b') te
In [9]: print "%s : %s" %('name','milo')
name : milo
In [8]: def f(x,y):
...: print "%s : %s" % x,y
...:
In [1]: def f(x,y):
...: print "%s : %s" % x ,y
...:
In [2]: t=('name','milo')
In [3]: f(t)
---------------------------------------------------------------------------
TypeError
正确
In [5]: def f(x,y):
...: print "%s : %s" % (x,y)
...:
In [6]: f(*t)
name : milo
怪死
In [9]: def f(name="name", age=0):
...: print "name: %s" %s name
File "<ipython-input-9-781b124a7d57>", line 2
print "name: %s" %s name
^
SyntaxError: invalid syntax
In [10]: def f(name="name", age=0):
...: print "name: %s" % name
...: print "age: %s" % age
...:
In [11]: f("evan",30)
name: evan
age: 30
In [12]: f()
name: name
age: 0
In [13]: t=(30,'evan')
In [14]: f(*t)
name: 30
age: evan
In [15]: d = {'age':30, 'name':'evan'}
In [16]: f(name="evan",age=30)
name: evan
age: 30
In [17]: f(age=30,name="evan")
name: evan
age: 30
In [1]: d = {'age':30, 'name':'evan'}
In [2]: def f(name="name", age=0):
...: print "name: %s" % name
...: print "age: %s" % age
...:
In [3]: f(**d)
name: evan
age: 30
In [4]: d['age']=31
In [5]: d
Out[5]: {'age': 31, 'name': 'evan'}
In [6]: f(**d)
name: evan
age: 31
In [7]: d={'a':30, 'n':'evan'}
In [8]: f(**d)
---------------------------------------------------------------------------
TypeError
In [9]: f(d['n'],d['a'])
name: evan
age: 30
In [10]: def f(x,*args):
...: print x
...: print args
...:
In [11]: f(1)
1
()
In [12]: f(1,2,3)
1
(2, 3)
In [13]: f(1,2,3,4,5,6)
1
(2, 3, 4, 5, 6)
In [14]: f(x=4)
4
()
In [15]: f(x=4,y=40)
---------------------------------------------------------------------------
TypeError
可以随意传输参数了:
In [16]: def f(x,*args,**kwargs):
...: print x
...: print args
...: print kwargs
...:
In [17]: f(1)
1
()
{}
n [18]: f(1,2,3,5)
1
(2, 3, 5)
{}
In [19]: f(x=1,y=2)
1
()
{'y': 2}
In [20]: f(1,3,4,x=1,y=2,z=20)
In [20]: f(1,3,4,x=1,y=2,z=20)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-0beb330e738b> in <module>()
----> 1 f(1,3,4,x=1,y=2,z=20)
TypeError: f() got multiple values for keyword argument 'x'
In [23]: f(1,3,4,z=1,y=2)
1
(3, 4)
{'y': 2, 'z': 1}
==20.函数lambda匿名函数==
In [1]: def f(x,y):
...: return x*y
...:
In [2]: g=lambda x,y:x*y
In [3]: g(2,3)
Out[3]: 6
In [4]: print g
<function <lambda> at 0x7fc9c0b54e60>
reduce
In [5]: def f(n):
...: if n > 0 :
...: n*f(n-1)
...:
In [6]: l = range(1,6)
In [7]: l
Out[7]: [1, 2, 3, 4, 5]
In [13]: def f(x,y):
...: return x*y
...:
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00141861202544241651579c69d4399a9aa135afef28c44000
把function 作用于list
n [14]: reduce(f,l)
Out[14]: 120
In [15]: f=lambda x,y:x*y
In [16]: reduce(f,l)
Out[16]: 120
== 21.Switch实现==
04:00
python的灵魂不在于语言本身,而在于其诸多优秀的库
如果做web,学一下django, flask等框架,看看它们的文档; 如果做游戏,看看pygame
总之,尝试做一个项目,在开发中和问题碰撞,并积累经验。光读书是没效果的。
Can’t guess python-indent-offset, using defaults: 4
python自动化运维开发【零基础】
http://study.163.com/course/introduction.htm?courseId=1003686002#/courseDetail?tab=1
链接:http://pan.baidu.com/s/1i4JidfB 密码:6tum
== 26.正则表达式(初识)==
pass 20170606
import re
#"" 有错呢 哈哈 为什么 呢
re.findall(s, 'aaaaa')
Out[5]:
[]
re.findall(s, 'aaaaabc')
Out[6]:
['abc']
st = "top ip tep "
res = r"top"
re.findall(res, st)
Out[10]:
['top']
#使用元字符
st = "top tip io "
res= r"t[io]p"
re.findall(res,st)
Out[9]:
['top', 'tip']
反
res= r"t[^io]p"
re.findall(res,st)
Out[11]:
[]
In [1]: s = "hello world, hello boy"
In [2]: r = r"hello"
In [4]: re.findall(r,s)
Out[4]: ['hello', 'hello']
In [5]: r = r"^hello"
In [6]: re.findall(r,s)
Out[6]: ['hello']
In [7]: s = "world,hello boy"
In [8]: re.findall(r,s)
Out[8]: []
***
In [10]: s = "world,hello boy"
In [11]: r=r"boy$"
In [12]: re.findall(r,s)
Out[12]: ['boy']
****
[] 中 $不起效果
In [13]: r = "t[abc$]"
In [14]: re.findall(r,'ta')
Out[14]: ['ta']
In [15]: re.findall(r,'tb')
Out[15]: ['tb']
In [16]: re.findall(r,'tax')
Out[16]: ['ta']
***
In [17]: re.findall(r,'t$')
Out[17]: ['t$']
In [18]: r = "t[abc^]"
In [19]: re.findall(r,'t^')
Out[19]: ['t^']
***
In [20]: r=r"x[0123456789]x"
In [21]: re.findall(r,'x1x')
Out[21]: ['x1x']
In [22]: re.findall(r,'x1x x2x')
Out[22]: ['x1x', 'x2x']
** 优化
In [23]: r = r"x[0-9]x"
In [24]: re.findall(r,'x1x x2x')
Out[24]: ['x1x', 'x2x']
==27 正则表达式(元字符) ==
配置电话号码
In [7]: import re
In [8]: r=r"^010-\d\d\d\d\d\d\d\d"
In [9]: re.findall(r,'010-87654321')
Out[9]: ['010-87654321']
In [10]: re.findall(r,'010-8765432')
Out[10]: []
升级
In [11]: r=r"^010-\d{8}"
In [12]: re.findall(r,'010-8765432')
Out[12]: []
In [13]: re.findall(r,'010-87654323')
Out[13]: ['010-87654323']
一个-或者没有-
r=r"^010-?\d{8}$"
非贪婪模式 一般加?
== 28.正则表达式(常用函数)==
import re
r1=r"\d{3,4}-?\d{8}"
In [4]: re.findall(r1,"010-12345678")
Out[4]: ['010-12345678']
In [5]: re.findall(r1,"010-1234567")
Out[5]: []
In [6]: p_tel=re.compile(r1)
In [7]: p_tel
Out[7]: re.compile(r'\d{3,4}-?\d{8}')
In [8]: p_tel.findall('010-223088198')
Out[8]: ['010-22308819']
In [9]: p_tel.findall('0101-223088198')
Out[9]: ['0101-22308819']
不区分大小写
In [11]: csvt_re = re.compile(r'csvt',re.I)
In [12]: csvt_re.findall('CSVT')
Out[12]: ['CSVT']
In [13]: csvt_re.findall('CSvt')
Out[13]: ['CSvt']
In [14]: csvt_re.match('csvt hello')
Out[14]: <_sre.SRE_Match at 0x7fcaf94c3510>
In [15]: csvt_re.match('hello')
In [16]: csvt_re.match('hello csvt')
In [17]: csvt_re.match('csvt')
Out[17]: <_sre.SRE_Match at 0x7fcaf94c3780>
In [18]: x = csvt_re.match('csvt')
In [19]: if x :
...: pass
...:
In [20]: csvt_re.search('hello csvt')
Out[20]: <_sre.SRE_Match at 0x7fcaf94c39f0>
In [21]: csvt_re.search('csvt hello')
Out[21]: <_sre.SRE_Match at 0x7fcaf94c3e68>
help(re.sub)
In [22]: s = "hello csvt"
In [24]: s.replace('csvt', 'python')
Out[24]: 'hello python'
更加好用的办法
In [30]: import re
In [31]: rs=r'c..t'
In [32]: re.sub(rs,'python','csvt caat cvvt cccc')
Out[32]: 'python python python cccc'
In [33]: re.subn(rs,'python','csvt caat cvvt cccc')
Out[33]: ('python python python cccc', 3)