python 技术点

1、file_time = date.strftime('%Y-%m-%d %H:%M:%S')  #时间格式转换

2、“ __”.join([1,2,3])     输出结果:1__2__3   

      “ __”.join(“123”)       输出结果:1__2__3   

3、__str__和__repr__区别                             #repr对象和print都行。

 class Item():
...     def __init__(self):
...         self.code = ''
...         self.data = ''
...     def __str__(self):
...         return 'caonimabi'
...     def __repr__(self):
...         return 'caonimaligebi'
... 
>>> a=Item()
>>> a
caonimaligebi
>>> print a
caonimabi
 

 

4、匿名函数lamdba

func = lambda x,y:x+y
print(func(1,2))

 

 

5、字典推导公式

gg = {1+i for i in [1,23,3]}
print(gg)

 

6、列表元素去重

⑴set([1,2,3,2,4])
⑵ss = ss.fromkeys(bb)           #
print(ss)
print(list(ss.keys()))

7、查询替换文本

import re

p = re.compile('blue|red|white')

prite(p.sub('colour','blue socks and red shoes',count=1))   #替换第一个

--》colour socks and red shoes

subn一样,不过返回二维数组。

8、match(),search()

re.match('super','superdf').span()

-->(0,5)

re.match('super','insuper')   #从开头搜索

-->None

search()   #搜索第一个匹配值

 

9、贪婪匹配:<.*>

非贪婪匹配:<.?*>