中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

hashlib、walk、yield

發布時間:2020-07-26 12:13:50 來源:網絡 閱讀:363 作者:1350368559 欄目:開發技術

一、hashlib 生成MD5值

[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]: <generator object f at 0x7fcb11732be0>
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)
<ipython-input-10-aa817a57a973> in <module>()
----> 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()時才運行函數生成一個結果


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

深圳市| 江门市| 清水县| 高密市| 灵山县| 商河县| 嘉义县| 古浪县| 郑州市| 海晏县| 潮安县| 冀州市| 尼玛县| 车险| 台安县| 桃源县| 香格里拉县| 福建省| 长泰县| 合川市| 墨江| 伽师县| 万州区| 神农架林区| 广南县| 县级市| 绥宁县| 青阳县| 延安市| 临澧县| 中超| 宾阳县| 大同市| 修文县| 绥棱县| 尖扎县| 黑水县| 沙田区| 霞浦县| 南川市| 夏河县|