- >>> line = '<IMG ALIGN="middle" SRC=\'#\'" /span>
- >>> mo=re.compile(r'(?<=SRC=)"([\w+\.]+)"',re.I)
- >>> mo.sub(r'"\1****"',line)
- '<IMG ALIGN="middle" SRC=\'#\'" /span>
- >>> mo.sub(r'replace_str_\1',line)
- '<IMG ALIGN="middle" replace_str_overview.gif BORDER="0" ALT="">'< /span>
- >>> mo.sub(r'"testetstset"',line)
- '<IMG ALIGN="middle" SRC=\'#\'" /span>
- import os
- fileList = []
- rootdir = "/data"
- for root, subFolders, files in os.walk(rootdir):
- if '.svn' in subFolders: subFolders.remove('.svn') # 排除特定目录
- for file in files:
- if file.find(".t2t") != -1:# 查找特定扩展名的文件
- file_dir_path = os.path.join(root,file)
- fileList.append(file_dir_path)
- print fileList
- >>> a = [('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0'), ('2011-03-15', '2.33', 15615500,'-19.1')]
- >>> print a[0][0]
- 2011-03-17
- >>> b = sorted(a, key=lambda result: result[1],reverse=True)
- >>> print b
- [('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0')]
- >>> c = sorted(a, key=lambda result: result[2],reverse=True)
- >>> print c
- [('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-16', '2.26', 12036900, '-3.0'), ('2011-03-17', '2.26', 6429600, '0.0')]
- >>> lst= [(1,'sss'),(2,'fsdf'),(1,'sss'),(3,'fd')]
- >>> set(lst)
- set([(2, 'fsdf'), (3, 'fd'), (1, 'sss')])
- >>>
- >>> lst = [1, 1, 3, 4, 4, 5, 6, 7, 6]
- >>> set(lst)
- set([1, 3, 4, 5, 6, 7])
- >>> from operator import itemgetter
- >>> aa = {"a":"1","sss":"2","ffdf":'5',"ffff2":'3'}
- >>> sort_aa = sorted(aa.items(),key=itemgetter(1))
- >>> sort_aa
- [('a', '1'), ('sss', '2'), ('ffff2', '3'), ('ffdf', '5')]
- >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
- >>> ["%s=%s" % (k, v) for k, v in params.items()]
- ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
- >>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
- 'server=mpilgrim;uid=sa;database=master;pwd=secret'
- >>> a = 'server=mpilgrim;uid=sa;database=master;pwd=secret'
- >>> aa = {}
- >>> for i in a.split(';'):aa[i.split('=',1)[0]] = i.split('=',1)[1]
- ...
- >>> aa
- {'pwd': 'secret', 'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'}
新一篇: 如何查看你电脑偷偷连接了什么 旧一篇: Centos5.6升级Python至2.7.5
当前暂无评论