0

I have a problem with .dat files in Python: I can not encode it. I have tried UTF-8, ASCII and many more.

import re

with open("mixture1.dat",'r', encoding="ascii", errors="surrogateescape") as f:
    lines = f.readlines()
    text = "".join(lines)

print(text)

Here is the link for the "mixture1.dat". There should be something related in chemistry but I could not open it for a week. How should I do it?

EDIT: SOLUTION

import pickle

def read_file(filename):
    with open(filename,  'rb')  as  FID:
        mp  = pickle.Unpickler(FID)
        data = mp.load()
    return data

Worked fine

2
  • 1
    Opening a binary file like that has no chance of success. strings says "numpy.core.multiarray". Check out stackoverflow.com/questions/20518632/… Commented Jan 1, 2021 at 7:55
  • What should i do then? I should open it and it corresponds to a mixture of two molecules Commented Jan 1, 2021 at 8:03

1 Answer 1

1

You can use numpy for this:

import numpy as np
data = np.fromfile('mixture1.dat', dtype=float)

print(data.size)
print(data[:20])

Output:

23767
[ 5.43235748e-312  7.01653493e-205  3.63521590e+228  9.77081644e+199
  4.03065734e-277 -2.37251204e-214  9.10016855e+276  4.27255706e+180
 -2.89898361e-211 -8.83065826e-211  3.49131717e+070  1.91561942e+053
 -3.80240360e-210  2.67555322e-318 -8.83065517e-211 -5.81601764e+181
 -5.71181552e-277  8.93904783e+014  3.37067979e-234  3.07882662e-292]

Sign up to request clarification or add additional context in comments.

4 Comments

What is the meaning of these numbers? Is it a header or the numbers in the file itself? Cause it was given that data is given in a binary file with 10001 values corresponding to the normalized fluorescence signal measured for 1 µs with a time step of 100 ps.
My understanding from the docs is that the numbers are the data in the file itself.
I have multiple that kind of file. Numbers in the even indexes are the same [ 1.10687736e+193 -3.04853105e+012 -8.83065517e-211 7.82932173e+185 -5.71172958e-277 8.93904783e+014 -1.21750579e+288 3.07882662e-292 3.90254972e+267 6.53306071e+089] [ 1.10687736e+193 -7.26428739e+192 -8.83065517e-211 3.21748662e+272 -5.71171472e-277 8.93904783e+014 -2.18020972e+271 3.07882662e-292 -1.42643138e-238 6.53306071e+089]
@SamadzadeMuhammed you should ask the meaning of the numbers to the people who created your .dat files. We can only help you decode the file, but we cannot tell you its meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.