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

  • 1
    Opening a binary file like that has no chance of success. strings says "numpy.core.multiarray". Check out https://stackoverflow.com/questions/20518632/importerror-numpy-core-multiarray-failed-to-import – Allan Wind Jan 01 '21 at 07:55
  • What should i do then? I should open it and it corresponds to a mixture of two molecules – Samadzade Muhammed Jan 01 '21 at 08:03

1 Answers1

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]

PApostol
  • 2,152
  • 2
  • 11
  • 21
  • 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. – Samadzade Muhammed Jan 01 '21 at 08:14
  • My understanding from the [docs](https://numpy.org/doc/stable/reference/generated/numpy.fromfile.html) is that the numbers are the data in the file itself. – PApostol Jan 01 '21 at 08:26
  • 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] – Samadzade Muhammed Jan 01 '21 at 08:53
  • 1
    @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. – pepoluan Jan 01 '21 at 12:48