189 8069 5689

hashlib、walk、yield-创新互联

一、hashlib 生成MD5值

成都创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业成都机柜租用,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。
[root@133 systeminformation]# vim hashlib2.py 
#!/usr/bin/env python
import hashlib
import sys
def md5sum(f):
    m = hashlib.md5()
    with open(f) as fd:
        while True:
            data = fd.read(4096)
            if data:
                m.update(data)
            else:
                break
    return m.hexdigest()
if __name__ == '__main__':
    try:
        print    md5sum(sys.argv[1])
    except IndexError:
        print "%s follow a argument" % __file__
        
[root@133 systeminformation]# python hashlib2.py 
hashlib2.py follow a argument
[root@133 systeminformation]# python hashlib2.py /etc/passwd
8cb5df95a0685c814cfacd0fef10dc1c

小试牛刀

 一个文件全部是手机号码,每行一个,需要将该文件的每个手机号码md5处理

[root@133 systeminformation]# vim 1_md5.py 
#!/usr/bin/env python                                                                                                                                                              
#coding=utf-8                                                                                                                                                                      
import hashlib                                                                                                                                                                     
import sys                                                                                                                                                                         
import os.path                                                                                                                                                                     
                                                                                                                                                                                   
def  md5sum(f):                                                                                                                                                                    
    f_out = open('/tmp/out.txt','w')                                                                                                                                               
    with open(f) as fd:                                                                                                                                                            
        for i  in  fd.readlines():                                                                                                                                                 
            line = i.strip('\n')                                                                                                                                                   
            md = hashlib.md5(line)                                                                                                                                                 
            f_out.write(md.hexdigest()+'\n')                                                                                                                                       
                                                                                                                                                                                   
if __name__ == '__main__':                                                                                                                                                         
    try:                                                                                                                                                                           
        print md5sum(sys.argv[1])                                                                                                                                                  
    except IndexError:                                                                                                                                                             
        print "%s follow a argument" % __file__  
        
[root@133 systeminformation]# python 1_md5.py 非码+支付宝会员_喜欢牛肉_手机号码.txt                                                                                                
None
[root@133 systeminformation]# head /tmp/out.txt 
1ed73d40234d99ef1ecfb00264af98e8
e85faa993080d67c5f791fdc015ad3d1
45a0515b1311341ae50c518999ba56e3
254d3e8963dc301e2d06a4fbacae6b76
5861016350aa8a9e2242113d40f0c5c5
aea768a8aac8c1e9ce74f7e9bb0a4a2b
34a5fdef10ea2c4d3300848837f05633
2e3fa16b751d1df711f6271554c8074c
caad46ce2ad1287ea8a60dd13e9314f4
d11598a9f78731f6911a1902990e8932

二、walk模块

os.walk

迭代目录里的文件

[root@133 systeminformation]# vim walk1.py 
#!/usr/bin/env python
import hashlib
import os
import sys
def md5sum(f):
    m = hashlib.md5()
    with open(f) as fd:
        while True:
            data = fd.read(4096)
            if data:
                m.update(data)
            else:
                break
    return m.hexdigest()
a = os.walk(sys.argv[1])
for p,d,f in a:
    for i in f:
        fn = os.path.join(p,i)
        md5 = md5sum(fn)
        print md5+'  '+fn
[root@133 systeminformation]# python walk1.py .
27f8b178ef14f5e79d4e875977c320f1  ./yield1.py
44ed2af7008a9e5bbd720495aaf07590  ./hashlib2.py
c38e72d0b260e35efc2d32dc75a7a34e  ./walk1.py
d41d8cd98f00b204e9800998ecf8427e  ./test/a
d41d8cd98f00b204e9800998ecf8427e  ./test/b

三、yield生成器

生成器是一个可迭代的对象,可以对可迭代对象进行遍历,比如字符串,列表等,都是可迭代对象

生成器对象

生成器是一个可迭代的对象,可以对可迭代对象进行遍历,比如字符串,列表等,都是可迭代对象

当使用for进行迭代的时候,函数内的代码才会被执行

mygenerator = (x*x for x in range(4))

next()方法

mygenerator.next()

[root@133 systeminformation]# vim yield1.py 
#!/usr/bin/env python
def h():
    print 'one'
    yield 1
    print 'two'
    yield 2
    print 'three'
    yield 3
a = h()
ipython
In [1]: def f(n):
   ...:     for i in range(n):
   ...:         yield i
   ...: 
In [11]: a
Out[11]: 
In [4]: a.next()
Out[4]: 0

In [5]: a.next()
Out[5]: 1

In [6]: a.next()
Out[6]: 2

In [7]: a.next()
Out[7]: 3

In [8]: a.next()
Out[8]: 4

In [9]: a.next()
Out[9]: 5

In [10]: a.next()
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
 in ()
----> 1 a.next()

In [14]: a = f(5)

In [15]: for i in a:print i
0
1
2
3
4

return与yield区别

return的时候这个函数的局部变量就都销毁了

所有return是得到所有结果之后的返回

yield是产生了一个可以恢复的函数(生成器),恢复了局部变量。

生成器只有在调用.next()时才运行函数生成一个结果

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:hashlib、walk、yield-创新互联
URL地址:http://jkwzsj.com/article/gecee.html

其他资讯