久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: av在线三级 | 日本精品在线播放 | 一级全黄视频 | 国产区精品在线观看 | 午夜免费在线 | 成人在线免费观看视频 | 午夜电影一区二区 | 精区3d动漫一品二品精区 | 九色 在线 | 99reav| 男女羞羞视频在线免费观看 | 一级少妇女片 | 欧美一区2区三区3区公司 | 欧美精品综合在线 | 成人网在线观看 | 日韩欧美视频在线 | 日韩成人免费视频 | 狼色网| 国产黄色av网站 | 国家aaa的一级看片 h片在线看 | 成年视频在线观看福利资源 | 成人亚洲在线 | 少妇特黄a一区二区三区88av | 麻豆视频在线免费观看 | 精品九九在线 | 91精品久久久久久久久久 | 久久天堂| 久久免费精品视频 | 久久久久国产一区二区 | 成人在线视频一区 | 天堂在线91 | 国产 日韩 欧美 中文 在线播放 | 国产欧美在线一区二区 | 亚洲成人综合网站 | 欧美二区在线 | 久久三区 | 国产精品色婷婷久久58 | 成人亚洲性情网站www在线观看 | 久久青青 | 成人国产在线观看 | 亚洲欧美综合 |