久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

迭代二進制文件的慣用方法是什么?

What is the idiomatic way to iterate over a binary file?(迭代二進制文件的慣用方法是什么?)
本文介紹了迭代二進制文件的慣用方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

使用文本文件,我可以這樣寫:

With a text file, I can write this:

with open(path, 'r') as file:
    for line in file:
        # handle the line

這等價于:

with open(path, 'r') as file:
    for line in iter(file.readline, ''):
        # handle the line

這個成語記錄在 PEP 234 但我沒有找到二進制文件的類似習語.

This idiom is documented in PEP 234 but I have failed to locate a similar idiom for binary files.

使用二進制文件,我可以這樣寫:

With a binary file, I can write this:

with open(path, 'rb') as file:
    while True:
        chunk = file.read(1024 * 64)
        if not chunk:
            break
        # handle the chunk

我嘗試過與文本文件相同的習語:

I have tried the same idiom that with a text file:

def make_read(file, size):
    def read():
        return file.read(size)
    return read

with open(path, 'rb') as file:
    for chunk in iter(make_read(file, 1024 * 64), b''):
        # handle the chunk

這是在 Python 中迭代二進制文件的慣用方式嗎?

Is it the idiomatic way to iterate over a binary file in Python?

推薦答案

我不知道有什么內置的方法可以做到這一點,但是一個包裝函數很容易寫:

I don't know of any built-in way to do this, but a wrapper function is easy enough to write:

def read_in_chunks(infile, chunk_size=1024*64):
    while True:
        chunk = infile.read(chunk_size)
        if chunk:
            yield chunk
        else:
            # The chunk was empty, which means we're at the end
            # of the file
            return

然后在交互式提示處:

>>> from chunks import read_in_chunks
>>> infile = open('quicklisp.lisp')
>>> for chunk in read_in_chunks(infile):
...     print chunk
... 
<contents of quicklisp.lisp in chunks>

當然,您可以輕松地將其調整為使用 with 塊:

Of course, you can easily adapt this to use a with block:

with open('quicklisp.lisp') as infile:
    for chunk in read_in_chunks(infile):
        print chunk

你可以像這樣去掉 if 語句.

And you can eliminate the if statement like this.

def read_in_chunks(infile, chunk_size=1024*64):
    chunk = infile.read(chunk_size)
    while chunk:
        yield chunk
        chunk = infile.read(chunk_size)

這篇關于迭代二進制文件的慣用方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 毛片一区 | 搞黄网站在线观看 | 欧美日韩在线免费观看 | 亚洲人成免费 | 亚洲社区在线 | 日韩精品一区二区三区视频播放 | 国产午夜精品久久久久免费视高清 | 成人网av| 91大神在线资源观看无广告 | av网站在线免费观看 | 久久久久国产精品 | 中文字幕11页 | 久久精品 | 黄色片网站在线观看 | 国产一级片一区二区 | 韩三级在线观看 | 手机三级电影 | www.久久精品视频 | 久久免费视频1 | 国产一区免费视频 | 第四色影音先锋 | 久久久久9999 | 日韩精品视频在线播放 | 99色在线视频 | 在线日韩精品视频 | 欧美日韩视频网站 | 中文字幕一区在线观看视频 | 精品欧美在线观看 | 手机av网 | 福利久久 | www久久99 | 黄色一级毛片免费看 | 在线观看成人av | 国产欧美一区二区三区久久人妖 | av日韩精品| 动漫www.被爆羞羞av44 | 一本久久a久久精品亚洲 | 精品视频在线播放 | 一区二区三区在线 | 成人高清在线 | av香港经典三级级 在线 |