Package for opening .dat files

Short question: I wanted to ask whether there are any inbuilt packages to load .dat files ?

.dat is not a standard format — it is a catch-all file extension that can be just about anything. So, to answer your question we’d need to know the specific program creating the .dat file and/or the specific format of the contents.

2 Likes

Oh, I actually wanted to get familiar with Julia so I can start contributing on Github, so decided to do my uni assignments (originally in R) using Julia. The .dat file is similar to csv.
I searched on the net for ways to load .dat files in Julia and this is what I had found…

hence my question…

If you would be able to share the first 10 lines of the .dat file it would be easier to answer your question…

You are right, I should have done that in the first place.
Here is the screenshot of the .dat file when I open it in notepad

image

Create a project:

mkdir myproject
cd myproject
julia --project="."

and execute this code:

using CSV
using DataFrames

df=DataFrame(CSV.File("data.dat", delim=" "))

Does it work?

3 Likes

This is working perfectly, Thank you!
The answer in stackexchange link suggested that this won’t work everytime, if the spacing between data is variable. That is why I wanted to know whether there are any standard methods.
Thank you for your help!

Alternatively you can also use Delimited Files · The Julia Language or GitHub - sl-solution/InMemoryDatasets.jl: Multithreaded package for working with tabular data in Julia . The first one for simple cases with low overhead, the second one allows you to define custom views on your data…

1 Like

Just pre-process the file to replace repeated spaces with a single space.

buf = IOBuffer(replace(read("data.dat", String), r" +" => ' '))
df=DataFrame(CSV.File(buf, delim=" "))
1 Like

You can also use the ignorerepeated keyword argument for CSV.jl (should avoid the extra pass through the data, which only matters if the file is huge): https://csv.juliadata.org/stable/reading.html#ignorerepeated

2 Likes

Small nitpick, if you do r" +" (this is supposed to have two spaces but discourse swallows one when displaying, so maybe yours was actually the same…) instead it will not replace single spaces with single spaces :slight_smile:

1 Like