How does python read dat file work if I don't know it's structure?

1.5K    Asked by ClareMatthews in Python , Asked on Feb 14, 2023

 Is there any way to at least read the text from the dat file? I have its corresponding mdf file hence I know what all data and columns are there in it. How do I figure out the contents in my dat file? Because all that I am getting currently is some gibberish even if I am opening it in binary mode.

from asammdf import MDF
dat_file = r"C:UsersHPO2KORDesktopWorkdata1.dat"
mdf_file = r"C:UsersHPO2KORDesktopWorkdata1.mdf"
df = mdf.to_dataframe()
mdf = MDF(mdf_file)
df.head()

which gives me enter image description here

How do I read the same data from the dat file, is there any library or code for the same? And when I am opening the same in binary mode I am getting

data = open(dat_file, "rb")
data.readlines()
which is giving me
b'|CF,2,1,1;|CK,1,3,1,1;rn',
 b'|NO,1,7,1,0,,0,;rn',
 b'|NL,1,10,1252,0x407;rn',
 b'|CT,1,41,0,6,Bench#,24,Korrosionstest 15A046-01,0,;rn',
 b'|CT,1,30,0,11,StartOfTest,8,06/30/17,0,;rn',
 b'|CT,1,58,0,10,ResultPath,36,c:\korrosionstest\daten\#170161-OR02,0,;rn',
 b'|CT,1,59,0,11,GraphicPath,36,c:\korrosionstest\daten\#170161-OR02,0,;rn',
 b'|CT,1,31,0,15,GraphicBaseName,5,736_2,0,;rn',
 b'|CT,1,26,0,10,PartNumber,5,736_2,0,;rn',
 b'|CT,1,31,0,9,VA-Nr. GS,11,170161-OR02,0,;rn',
 b'|CT,1,62,0,9,VA-Nr. CC,42,TO_ENV_2017_G2_C1_Platform_CC-122164-03-08,0,;rn',
 b'|CT,1,24,0,6,Tester,8,Behrendt,0,;rn',
 b'|CT,1,32,0,15,Test Department,6,GS/ETR,0,;rn',
 b'|CG,1,5,1,1,1;rn',
 b'|CD,1,16,1E-2,1,1,s,0,0,0;rn',
 b'|NT,1,27,30, 6,2017,14,25,15.8050001;rn',
 b'|CC,1,3,1,1;rn',
 b'|CP,1,16,1,2,4,16,0,0,1,0;rn',
 b'|Cb,1,33,1,0,1,1,0,11718,0,11718,1,5E-3,0,;rn',
 b'|CR,1,30,1,6.103888176768602E-3,0,1,1,A;rn',
 b'|CN,1,28,0,0,0,16,ai_iB1_Strom_ECU,0,;rn',
 b'|CG,1,5,1,1,1;rn',
 b'|CD,1,16,1E-2,1,1,s,0,0,0;rn',
 b'|NT,1,27,30, 6,2017,14,25,15.8050001;rn',
 b'|CC,1,3,1,1;rn',
 b'|CP,1,16,2,2,4,16,0,0,1,0;rn',
 b'|Cb,1,37,1,0,2,1,11718,11718,0,11718,1,5E-3,0,;rn',
 b'|CR,1,30,1,3.662332906061161E-3,0,1,1,V;rn',
 b'|CN,1,31,0,0,0,19,ai_iB1_Spannung_UBB,0,;rn',
 b'|CG,1,5,1,1,1;rn',
 b'|CD,1,16,1E-2,1,1,s,0,0,0;rn',
 b'|NT,1,27,30, 6,2017,14,25,15.8050001;rn',
 b'|CC,1,3,1,1;rn',
Answered by David EDWARDS
For python read dat file, you can try:
np.fromfile(yourfilepath, dtype="byte")
to read content from .dat and then load as a Numpy array.


Your Answer

Interviews

Parent Categories