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

在 Python 中搜索/讀取二進制數據

Searching/reading binary data in Python(在 Python 中搜索/讀取二進制數據)
本文介紹了在 Python 中搜索/讀取二進制數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在讀取二進制文件(在本例中為 jpg),并且需要在該文件中找到一些值.對于那些感興趣的人,二進制文件是一個 jpg,我試圖通過尋找二進制結構來挑選它的尺寸 詳細在這里.

I'm reading in a binary file (a jpg in this case), and need to find some values in that file. For those interested, the binary file is a jpg and I'm attempting to pick out its dimensions by looking for the binary structure as detailed here.

我需要在二進制數據中找到 FFC0,向前跳過一些字節,然后讀取 4 個字節(這應該給我圖像尺寸).

I need to find FFC0 in the binary data, skip ahead some number of bytes, and then read 4 bytes (this should give me the image dimensions).

在二進制數據中搜索值的好方法是什么?是否有相當于find"或類似 re 的東西?

What's a good way of searching for the value in the binary data? Is there an equivalent of 'find', or something like re?

推薦答案

您實際上可以將文件加載到一個字符串中,并使用 str.find 在該字符串中搜索字節序列 0xffc0() 方法.它適用于任何字節序列.

You could actually load the file into a string and search that string for the byte sequence 0xffc0 using the str.find() method. It works for any byte sequence.

執行此操作的代碼取決于幾件事.如果您以二進制模式打開文件并且使用的是 Python 3(這兩種方法都可能是這種情況下的最佳實踐),您需要搜索字節字符串(而不是字符串),這意味著您必須在字符串前面加上 b.

The code to do this depends on a couple things. If you open the file in binary mode and you're using Python 3 (both of which are probably best practice for this scenario), you'll need to search for a byte string (as opposed to a character string), which means you have to prefix the string with b.

with open(filename, 'rb') as f:
    s = f.read()
s.find(b'xffxc0')

如果您在 Python 3 中以文本模式打開文件,則必須搜索字符串:

If you open the file in text mode in Python 3, you'd have to search for a character string:

with open(filename, 'r') as f:
    s = f.read()
s.find('xffxc0')

雖然沒有特別的理由這樣做.與以前的方式相比,它不會給您帶來任何優勢,并且如果您使用的平臺處理二進制文件和文本文件的方式不同(例如 Windows),這可能會導致問題.

though there's no particular reason to do this. It doesn't get you any advantage over the previous way, and if you're on a platform that treats binary files and text files differently (e.g. Windows), there is a chance this will cause problems.

Python 2 沒有區分字節串和字符串,所以如果你使用那個版本,在 中包含還是排除 b 都沒有關系b'xffxc0'.而且,如果您的平臺對二進制文件和文本文件的處理方式相同(例如 Mac 或 Linux),則無論您使用 'r' 還是 'rb' 作為文件都沒有關系模式.但我仍然建議使用類似于上面第一個代碼示例的東西,只是為了向前兼容 - 如果您確實切換到 Python 3,那么修復它就少了一件事情.

Python 2 doesn't make the distinction between byte strings and character strings, so if you're using that version, it doesn't matter whether you include or exclude the b in b'xffxc0'. And if your platform treats binary files and text files identically (e.g. Mac or Linux), it doesn't matter whether you use 'r' or 'rb' as the file mode either. But I'd still recommend using something like the first code sample above just for forward compatibility - in case you ever do switch to Python 3, it's one less thing to fix.

這篇關于在 Python 中搜索/讀取二進制數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久福利电影 | 激情一区二区三区 | 91中文在线观看 | 国产伦精品一区二区三区精品视频 | 亚洲综合视频一区 | 五月免费视频 | 欧美日韩一区二区三区视频 | 高清一区二区 | 综合国产第二页 | 成人免费大片黄在线播放 | 国产精品久久久久久久久免费 | 一区二区福利视频 | 精品免费国产视频 | 超碰欧美| 美女一级a毛片免费观看97 | 日韩精品一区二区三区高清免费 | 欧美一区二区久久 | 少妇无套高潮一二三区 | av网站在线播放 | 视频一区二区在线观看 | 欧美女优在线观看 | 成人av网页| 中国美女一级黄色片 | 免费在线观看黄网站 | 免费成人午夜 | 日韩欧美一区二区三区免费观看 | 免费看的av | 亚洲综合视频一区 | 亚洲最新在线 | 亚洲国产精品久久久 | 亚洲一区二区不卡在线观看 | 在线一级片 | 国产美女精品 | 天天射影院| 亚洲精品视频免费 | 欧美日韩国产在线观看 | 日韩欧美国产一区二区三区 | 久久久这里都是精品 | 一区二区视频 | 久久手机视频 | 久久视频精品在线 |