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

用python讀取二進(jìn)制文件

Reading a binary file with python(用python讀取二進(jìn)制文件)
本文介紹了用python讀取二進(jìn)制文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我發(fā)現(xiàn)用 Python 讀取二進(jìn)制文件特別困難.你能幫我個忙嗎?我需要閱讀這個文件,它在 Fortran 90 中很容易被閱讀

I find particularly difficult reading binary file with Python. Can you give me a hand? I need to read this file, which in Fortran 90 is easily read by

int*4 n_particles, n_groups
real*4 group_id(n_particles)
read (*) n_particles, n_groups
read (*) (group_id(j),j=1,n_particles)

具體來說,文件格式是:

In detail, the file format is:

Bytes 1-4 -- The integer 8.
Bytes 5-8 -- The number of particles, N.
Bytes 9-12 -- The number of groups.
Bytes 13-16 -- The integer 8.
Bytes 17-20 -- The integer 4*N.
Next many bytes -- The group ID numbers for all the particles.
Last 4 bytes -- The integer 4*N. 

如何使用 Python 閱讀此內(nèi)容?我嘗試了一切,但從未奏效.我有沒有機(jī)會在 python 中使用 f90 程序,讀取這個二進(jìn)制文件,然后保存我需要使用的數(shù)據(jù)?

How can I read this with Python? I tried everything but it never worked. Is there any chance I might use a f90 program in python, reading this binary file and then save the data that I need to use?

推薦答案

讀取二進(jìn)制文件內(nèi)容如下:

Read the binary file content like this:

with open(fileName, mode='rb') as file: # b is important -> binary
    fileContent = file.read()

然后使用struct.unpack解壓"二進(jìn)制數(shù)據(jù):

then "unpack" binary data using struct.unpack:

起始字節(jié):struct.unpack("iiiiii", fileContent[:20])

正文:忽略標(biāo)題字節(jié)和尾隨字節(jié)(= 24);剩下的部分構(gòu)成正文,要知道正文中的字節(jié)數(shù),進(jìn)行整數(shù)除以 4;得到的商乘以字符串 'i' 為 unpack 方法創(chuàng)建正確的格式:

The body: ignore the heading bytes and the trailing byte (= 24); The remaining part forms the body, to know the number of bytes in the body do an integer division by 4; The obtained quotient is multiplied by the string 'i' to create the correct format for the unpack method:

struct.unpack("i" * ((len(fileContent) -24) // 4), fileContent[20:-4])

結(jié)束字節(jié):struct.unpack("i", fileContent[-4:])

這篇關(guān)于用python讀取二進(jìn)制文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: av中文字幕在线 | 一级黄色av电影 | 综合成人在线 | 亚洲一区二区三区在线视频 | 国产成人精品网站 | 亚洲国产情侣自拍 | 99中文字幕 | 天堂一区二区三区 | 国产精品3区 | 国产一区二区 | aaa在线| 欧美精品一区二区三区蜜桃视频 | 亚洲综合婷婷 | 天天色综网 | 午夜免费电影院 | 国产精品永久久久久 | 久久国产精品91 | 日韩超碰在线 | аⅴ资源新版在线天堂 | 午夜视频在线免费观看 | 在线中文字幕亚洲 | 国产免费一区 | 国产精品欧美一区二区 | 久久中文字幕av | 午夜影视在线观看 | 欧美亚洲网站 | 中文字幕一区二区三区四区五区 | 天天拍天天操 | 一本色道精品久久一区二区三区 | 久久久精品网站 | 精品久久久久久久久久久久久 | 99久久久久 | 国产伦一区二区三区久久 | 99久久婷婷国产综合精品电影 | 国产精品激情小视频 | 国产一区视频在线 | 精品免费国产视频 | 免费一区| 亚洲国产精品久久 | 国产成人免费 | 国产成人免费 |