1. 程式人生 > >Python每日一練0022

Python每日一練0022

問題

你想在一個檔案裡每次讀入固定大小的位元組,比如每次讀入4個位元組並轉成int,或者每次讀入x個位元組並進行結構化,例如:

l = [5, 2, 4, 1, 2, 4, 5, 6, 8]
with open('test', 'wb') as fd:
    for num in l:
        fd.write(num.to_bytes(4, 'big'))

解決方案

可以簡單的用while迴圈來完成

with open('test', 'rb') as fd:
    r = fd.read(4)
    while r:
        print(int.from_bytes(r, 'big'
), end=' ') r = fd.read(4)

輸出為5 2 4 1 2 4 5 6 8

但更優雅的做法是結合使用iterfunctools.partial

from functools import partial
with open('test', 'rb') as fd:
    for r in iter(partial(fd.read, 4), b''):
        print(int.from_bytes(r, 'big'), end=' ')

輸出同樣是5 2 4 1 2 4 5 6 8

討論

functools.partial的作用是對一個函式進行包裝(可以將一些引數的值固定)並生成一個新的簽名,例如:

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18

functools.partial的大致實現如下:

def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        newkeywords.update(fkeywords)
        return
func(*args, *fargs, **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc

iter的作用是當只有一個引數時,對這個物件進行迭代,所以這個物件必須實現__iter()____getitem()__方法,例如:

>>> l = [1, 2, 3, 4]
>>> for i in iter(l):
...     print(i, end=' ')
...
>>> 1 2 3 4

但當有兩個引數時,第一個引數必須是可呼叫的(比如函式),第二個引數是終止的值,當呼叫第一個引數返回的結果等於第二個引數時,迭代就停止了,例如:

with open('mydata.txt') as fp:
    for line in iter(fp.readline, ''):
        process_line(line)

所以綜合兩個方法來看上面的iter(partial(fd.read, 4), b''),就是每次呼叫fd.read(4)直到返回一個空的bytes

來源

Python Cookbook

關注

歡迎關注我的微信公眾號:python每日一練