pythondat-protocol

Reading a .dat file in Python corrupts the file


I have a dat file of 3D dimensional coordinates that I am trying to read like this:

path = '/path/to/dat-file.dat'

data_content = [i.strip().split() for i in open(path, encoding = 'ISO-8859-1').readLines()]
print(data_content)

This is my output:

...\xad\x05jR|APcAoSNvA07\x9...

Its basically a long cryptic line with letters that have accents as well as letters from the cyrillic alphabet.

Is the way I'm opening this file corrupting it? Where am I going wrong?


Solution

  • .dat files contain binary data, not text data, but print() only works with text data. The file is not being corrupted, you are just printing the data in the wrong format. If you want to print the data inside the .dat file and get something meaningful, then you will need to use a library that understands the .dat file format.