Skip to content Skip to sidebar Skip to footer

Reading .dat File In Python

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

Solution 1:

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

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

withopen("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 inenumerate(matches, start=1):
    for group in match.groups():
        data.append(group.split(","))

for d in data:
    print(d)

Input

|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;

Output

['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 ;.

Edit

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 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.

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

Solution 2:

You could use the following code:

infile = open('.input.dat', 'r')
for line in infile:
    # Typical line: variable = value
    variable, value = line.split('=')
    variable = variable.strip()  # remove leading/traling blanks
infile.close()

There is more information here: file reading

Post a Comment for "Reading .dat File In Python"