# Package for opening .dat files

**URL:** <https://discourse.julialang.org/t/package-for-opening-dat-files/91472>\
**Category:** New to Julia\
**Tags:** question, package, io\
**Created:** [December 9, 2022, 12:07pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472 "2022-12-09T12:07:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [December 9, 2022, 12:07pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/1 "2022-12-09T12:07:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 9, 2022, 12:37pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/2 "2022-12-09T12:37:31Z")

</div>

`.dat` is not a standard format — it is a [catch-all file extension](https://stackoverflow.com/questions/41437816/what-is-the-difference-between-bin-and-dat-file) 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.

---

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [December 9, 2022, 1:11pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/3 "2022-12-09T13:11:11Z")

</div>

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…

> <https://stackoverflow.com/questions/61665998/reading-a-dat-file-in-julia-issues-with-variable-delimeter-spacing>

hence my question…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 9, 2022, 1:16pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/4 "2022-12-09T13:16:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [December 9, 2022, 1:28pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/5 "2022-12-09T13:28:15Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a31e10c6a29e4e678aefc1ae6c4421ebda9c611.png)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 9, 2022, 1:36pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/6 "2022-12-09T13:36:35Z")

</div>

Create a project:

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

```

and execute this code:

```julia
using CSV
using DataFrames

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

```

Does it work?

---

<div class="post-metadata">

**Author:** ![Rajat\_Chaudhari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajat_chaudhari/32/32783_2.png) [@Rajat\_Chaudhari](https://discourse.julialang.org/u/Rajat_Chaudhari)\
**Post date:** [December 9, 2022, 1:59pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/7 "2022-12-09T13:59:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 9, 2022, 3:50pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/8 "2022-12-09T15:50:24Z")

</div>

Alternatively you can also use [Delimited Files · The Julia Language](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/) or [GitHub - sl-solution/InMemoryDatasets.jl: Multithreaded package for working with tabular data in Julia](https://github.com/sl-solution/InMemoryDatasets.jl) . The first one for simple cases with low overhead, the second one allows you to define custom views on your data…

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 9, 2022, 4:30pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/9 "2022-12-09T16:30:12Z")

</div>

> [@Rajat\_Chaudhari](#):
>
> The answer in stackexchange link suggested that this won’t work everytime, if the spacing between data is variable.

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

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

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [December 9, 2022, 4:44pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/10 "2022-12-09T16:44:08Z")

</div>

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](https://csv.juliadata.org/stable/reading.html#ignorerepeated)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 9, 2022, 6:42pm UTC](https://discourse.julialang.org/t/package-for-opening-dat-files/91472/11 "2022-12-09T18:42:59Z")

</div>

> [@stevengj](#):
>
> `r" +"`

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 🙂
