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

用python讀取二進制文件

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

問題描述

我發現用 Python 讀取二進制文件特別困難.你能幫我個忙嗎?我需要閱讀這個文件,它在 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 閱讀此內容?我嘗試了一切,但從未奏效.我有沒有機會在 python 中使用 f90 程序,讀取這個二進制文件,然后保存我需要使用的數據?

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?

推薦答案

讀取二進制文件內容如下:

Read the binary file content like this:

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

然后使用struct.unpack解壓"二進制數據:

then "unpack" binary data using struct.unpack:

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

正文:忽略標題字節和尾隨字節(= 24);剩下的部分構成正文,要知道正文中的字節數,進行整數除以 4;得到的商乘以字符串 'i' 為 unpack 方法創建正確的格式:

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])

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

這篇關于用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 | 日韩成人av在线 | 欧美日韩不卡 | 天天操夜夜操 | 久久国产一区 | 久久三级av | 亚洲精品一区二区三区在线 | 福利网址 | 国产国产精品 | 久久这里只有精品首页 | 91精品国产一区二区三区 | 午夜精品视频在线观看 | 亚洲欧洲综合av | 成人乱人乱一区二区三区软件 | 玩丰满女领导对白露脸hd | 国产精品1区 | 麻豆久久久久久 | 青青99 | 亚洲人成人一区二区在线观看 | 欧洲色综合 | 国产精品免费福利 | 日韩一区二区在线看 | 久久国产精品免费一区二区三区 | 国产精品1区2区 | 精品国产久 | 国产午夜精品一区二区三区四区 | 日本中文字幕在线观看 | 99久久婷婷国产综合精品首页 | 亚洲精品永久免费 | 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | 亚洲成人精选 | 91精品久久久| 欧美xxxx日本 | www.久久 | 国产日韩欧美在线观看 | 国产精品不卡一区 | 一区二区三区四区国产精品 | 可以免费看的毛片 |