0

I am writing a program in Python which should import *.dat files, subtract a specific value from certain columns and subsequently save the file in *.dat format in a different directory.

My current tactic is to load the datafiles in a numpy array, perform the calculation and then save it. I am stuck with the saving part. I do not know how to save a file in python in the *.dat format. Can anyone help me? Or is there an alternative way without needing to import the *.dat file as a numpy array? Many thanks!

STJ
  • 113
  • 1
  • 2
  • 12
  • 1
    Do you know how to save *any* files in Python? If not - have you tried consulting the Python documentation? If not, I'd recommend doing that or checking out [this question](https://stackoverflow.com/questions/9536714/python-save-to-file). – alex Oct 31 '17 at 14:46
  • There is no specific "dat" format. – Aleksandr Borisov Oct 31 '17 at 14:46
  • Can you specify what dat format are you talking about? It is a binary file? or text file with some specific format? Really there is no standard dat format... you can create your own format if you want to – Geomorillo Oct 31 '17 at 14:47

5 Answers5

4

You can read and export a .dat file using pandas:

import pandas as pd
input_df = pd.read_table('input_file_name.dat')  
...  
output_df = pd.DataFrame({'column_name': column_values})  
output_df.to_csv('output_file_name.dat')
Ale
  • 917
  • 9
  • 28
3

assuming your file looks like

file = open(filename, "r")

all you need to do is open another file with "w" as the second parameter

file = open(new_file-path,"w")
file.write(data)
file.close()

if your data is not a string, either make it a string, or use

file = open(filename, "rb")
file = open(filename, "wb")

when reading and writing, since these read and write raw bytes

Yardid
  • 247
  • 1
  • 6
3

You can use struct to pack the integers in a bytes format and write them to a dat file.

import struct

data = [# your data]

Open:

with open('your_data.dat', 'rb') as your_data_file:
    values = struct.unpack('i'*len(data), your_data_file.read())

Save data:

with open('your_data.dat', 'wb') as your_dat_file:  
    your_dat_file.write(struct.pack('i'*len(data), *data))

Reference.

toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
0

The .dat file can be read using the pandas library:

df = pd.read_csv('xxxx.dat', sep='\s+', header=None, skiprows=1)

skiprows=1 will ignore the first row, which is the header.

\s+ is the separation (default) of .dat file.

TYZ
  • 8,466
  • 5
  • 29
  • 60
0

Correct me if I'm wrong, but opening, writing to, and subsequently closing a file should count as "saving" it. You can test this yourself by running your import script and comparing the last modified dates.

JCJ
  • 303
  • 3
  • 13