开发者

Reading a Delphi binary file in Python

开发者 https://www.devze.com 2022-12-27 18:48 出处:网络
I have a file that was written with the following Delphi declaration ... Type Tfulldata = Record dpoints, dloops : integer;

I have a file that was written with the following Delphi declaration ...


Type
  Tfulldata = Record
    dpoints, dloops : integer;
    dtime, bT, sT, hI, LI : real;
    tm : real;
    data : array[1..armax] Of Real;
  End;

...
Var:
  fh: File Of Tfulldata;

I want to analyse the data in the files (many MB in size) using Python开发者_如何学运维 if possible - is there an easy way to read in the data and cast the data into Python objects similar in form to the Delphi records? Does anyone know of a library perhaps that does this?

This is compiled on Delphi 7 with the following options which may (or may not) be pertinent,

  • Record Field Alignment: 8
  • Pentium Safe FDIV: False
  • Stack Frames: False
  • Optimization: True


Here is the full solutions thanks to hints from KillianDS and Ritsaert Hornstra

import struct
fh = open('my_file.dat', 'rb')
s = fh.read(40256)
vals = struct.unpack('iidddddd5025d', s)
dpoints, dloops, dtime, bT, sT, hI, LI, tm = vals[:8]
data = vals[8:]


I do not know how Delphi internally stores data, but if it is as simple byte-wise data (so not serialized and mangled), use struct. This way you can treat a string from a python file as binary data. Also, open files as binary file(open,'rb').


Please note that when you define a record in Delphi (like struct in C) the fields are layed out in order and in binary given the current alignment (eg Bytes are aligned on 1 byte boundaries, Words on 2 byte, Integers on 4 byte etc, but it may vary given the compiler settings.

When serialized to a file, you probably mean that this record is written in binary to the file and the next record is written after the first one starting at position sizeof( structure) etc etc. Delphi does not specify how thing should be serialized to/from file, So the information you give leaves us guessing.

If you want to make sure it is always the same without interference of any compiler setings, use packed record.

Real can have multiple meanings (it is an 48 bit float type for older Delphi versions and later on a 64 bit float (IEEE double)).

If you cannot access the Delphi code or compile it yourself, just ty to check the data with a HEX editor, you should see the boundaries of the records clearly since they start with Integers and only floats follow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号