在python中读取.dat文件 [英] Reading .dat file in python

查看:581
本文介绍了在python中读取.dat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.dat文件,我对该文件的创建方式,使用的定界符以及有关它的任何细节一无所知.我只是有其对应的mdf和csv文件.就这样.python中有什么方法可以读取此.dat文件吗?

I have a .dat file which I don't have any idea about how it was created and what delimiter was used and any details about it. I just have its corresponding mdf and csv file. Thats all. Is there any way in python to read this .dat file?

我尝试过的几种方法:

file = "736_2_PerformanceCurve_(23_0C)_(13_5V).dat"
datContent = [i.strip().split() for i in open(file, encoding='latin1').readlines()]
datContent

给出输出

[['|CF,2,1,1;|CK,1,3,1,1;'],
 ['|NO,1,7,1,0,,0,;'],
 ['|NL,1,10,1252,0x407;'],
 ['|CT,1,41,0,6,Bench#,24,Korrosionstest', '15A046-01,0,;'],
 ['|CT,1,30,0,11,StartOfTest,8,06/30/17,0,;'],
 ['|CT,1,58,0,10,ResultPath,36,c:\\korrosionstest\\daten\\#170161-OR02,0,;'],
 ['|CT,1,59,0,11,GraphicPath,36,c:\\korrosionstest\\daten\\#170161-OR02,0,;'],
 ['|CT,1,31,0,15,GraphicBaseName,5,736_2,0,;'],
 ['|CT,1,26,0,10,PartNumber,5,736_2,0,;'],
 ['|CT,1,31,0,9,VA-Nr.', 'GS,11,170161-OR02,0,;'],
 ['|CT,1,62,0,9,VA-Nr.',
  'CC,42,TO_ENV_2017_G2_C1_Platform_CC-122164-03-08,0,;'],
 ['|CT,1,24,0,6,Tester,8,Behrendt,0,;'],
 ['|CT,1,32,0,15,Test', 'Department,6,GS/ETR,0,;'],
 ['|CG,1,5,1,1,1;'],
 ['|CD,1,16,1E-2,1,1,s,0,0,0;'],
 ['|NT,1,27,30,', '6,2017,14,25,15.8050001;'],
 ['|CC,1,3,1,1;'],
 ['|CP,1,16,1,2,4,16,0,0,1,0;'],
 ['|Cb,1,33,1,0,1,1,0,11718,0,11718,1,5E-3,0,;'],
 ['|CR,1,30,1,6.103888176768602E-3,0,1,1,A;'],
 ['|CN,1,28,0,0,0,16,ai_iB1_Strom_ECU,0,;'],
 ['|CG,1,5,1,1,1;'],
 ['|CD,1,16,1E-2,1,1,s,0,0,0;'],
 ['|NT,1,27,30,', '6,2017,14,25,15.8050001;'],
 ['|CC,1,3,1,1;'],
 ['|CP,1,16,2,2,4,16,0,0,1,0;'],
 ['|Cb,1,37,1,0,2,1,11718,11718,0,11718,1,5E-3,0,;'],
 ['|CR,1,30,1,3.662332906061161E-3,0,1,1,V;'],
 ['|CN,1,31,0,0,0,19,ai_iB1_Spannung_UBB,0,;'],

与之对应的csv文件

[![在此处输入图片描述] [1]] [1]

[![enter image description here][1]][1]

from asammdf import MDF
dat_file = r"C:\Users\HPO2KOR\Desktop\Work\data1.dat"
mdf_file = r"C:\Users\HPO2KOR\Desktop\Work\data1.mdf"
df = mdf.to_dataframe()
mdf = MDF(mdf_file)
df.head()

这给了我[![在此处输入图片描述] [2]] [2]

which gives me [![enter image description here][2]][2]

如何从dat文件中读取相同的数据,是否有相同的库或代码?

How do I read the same data from the dat file, is there any library or code for the same?

推荐答案

如果我查看该文件,它将以一种特定的格式显示给我.

If I look at the file it looks for me like a specific format.

一个数据块以 | 开头,以; 结尾.在数据块中,数据用分割.基本上就像CSV,但是换行符是; .

One data block starts with a | and ends with a ;. In the data block the data are splitted with ,. Basically it's like a CSV but the newline is ;.

现在,借助正则表达式,您可以像下面这样读取数据:

Now with the help of regex you can read this data like this:

import re

with open("resources/input.dat") as f:
    lines = f.readlines()
    text = "".join(lines)

regex = r"\|(.*?);"
matches = re.finditer(regex, text, re.MULTILINE | re.DOTALL)


data = []

for matchNum, match in enumerate(matches, start=1):
    for group in match.groups():
        data.append(group.split(","))

for d in data:
    print(d)

输入

|CF,2,1,1;|CK,1,3,1,1;
|NO,1,7,1,0,,0,;
|CT,1,41,0,6,Bench,24,Korrosionstest', '15A046-01,0,
otherline_data;

输出

['CF', '2', '1', '1']
['CK', '1', '3', '1', '1']
['NO', '1', '7', '1', '0', '', '0', '']
['CT', '1', '41', '0', '6', 'Bench', '24', "Korrosionstest'", " '15A046-01", '0', '\notherline_data']

您可以看到,即使数据块没有在新行结束,您仍然可以获取数据,直到定义的结束标记; .

As you can see even if the data block doesn't end at a new line, you still get the data until the defined end mark ;.

我下载了您的.dat文件.正如您在第1133行之后看到的那样,有些奇怪的字符根本没有任何意义.此字符或字节可能是您在一开始处理数据所需的信息.基本上,它看起来像一些压缩的数据,其中包含我在评论中告知您的所需背景信息.

I downloaded your .dat file. As you can see after line 1133 there are strange characters that doesn't make sense at all. This characters or rather bytes are probably the information you need to process the data in the beginning. Basically it looks like some compressed data with the needed background information I informed you in the comment.

FAMOS具有解释该字节字符串的知识,因此可以按预期向您提供数据.如何解释呢?询问来源,您可以从哪里获得数据或在FAMOS代码中找到它.

FAMOS has the knowledge to interpret that byte string and therefore can present you with the data as it is intented. How to interpret this? Ask the source where you get the data or find it in the FAMOS code.

我认为这里没有人可以回答您.而且我不知道如何.这太具体了,因此最好去获取数据的地方.

I don't think somebody here can answer you this. And I don't know how. This is too specific and therefore it is better to go where you get the data.

.dat文件中的代码段:(总共32404行,只有1133行包含数据)

A snippet from the .dat file: (In total 32404 lines and only 1133 with data)

这篇关于在python中读取.dat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆