# Reading a txt. file with two different delimiters

**URL:** <https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087>\
**Category:** General Usage\
**Tags:** csv\
**Created:** [May 15, 2022, 9:01am UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087 "2022-05-15T09:01:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)\
**Post date:** [May 15, 2022, 9:01am UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/1 "2022-05-15T09:01:54Z")

</div>

Hey guys,

ich want to read in the following text file.  
 ![Bildschirmfoto 2022-05-15 um 10.51.40](https://global.discourse-cdn.com/julialang/original/3X/0/2/020b2efee3e1d53eb683a100f36373553011af09.png)

So the first two rows are irrelevant, but the problem is that the file has space and ; as a delimiter. If I try to read in the file I get the following result:

 ![Bildschirmfoto 2022-05-15 um 10.58.46](https://global.discourse-cdn.com/julialang/original/3X/0/4/04abf7a75dedcc33f1d2e06d1bdb80659eada351.png)

Now the problem is that the third column is now a string because the integer and the ; gets combined in a string.

Does someone has an idea of how to read in a file like this properly?

Thanks!

---

<div class="post-metadata">

**Author:** ![mastrof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mastrof/32/17443_2.png) [@mastrof](https://discourse.julialang.org/u/mastrof)\
**Post date:** [May 15, 2022, 2:06pm UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/2 "2022-05-15T14:06:09Z")

</div>

If you don’t need to use `CSV` package in particular, with `readdlm` from `DelimitedFiles` you could do  
`readdlm(filename, Int, skipstart=2, comments=true, comment_char=';')`  
It will read anything after a `;` character (included) as a comment and skip it.

I don’t know much about `CSV` but it seems it to have optional arguments for comments that you might try to use in the same way: [Reading · CSV.jl](https://csv.juliadata.org/stable/reading.html#comment)  
Although here it says that the comment character should be at the start of the line…

---

<div class="post-metadata">

**Author:** ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)\
**Post date:** [May 15, 2022, 2:32pm UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/3 "2022-05-15T14:32:54Z")

</div>

> [@mastrof](#):
>
> readdlm(filename, Int, skipstart=2, comments=true, comment\_char=‘;’)

Thank you! That works for me.

Yes I’ve seen the optional arguments for CSV.File but none of these options worked in my case. At least I haven’t found the right one I guess.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 15, 2022, 2:47pm UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/4 "2022-05-15T14:47:08Z")

</div>

Related:

[https://github.com/JuliaData/CSV.jl/issues/956](https://github.com/JuliaData/CSV.jl/issues/956)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 15, 2022, 3:11pm UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/5 "2022-05-15T15:11:30Z")

</div>

You could try (this is likely only useful to you, faster, if your file is huge):

```julia
CSV.File(filname; delim=' ', decimal = ';')

```

It’s a bit of a hack, with decimal, used as a comments hack, and then make sure your values are actually always integer going in (and what you get). If you tried with the comment keyword arg and it didn’t work then likely because:

> [@mastrof](#):
>
> Although here it says that the comment character should be at the start of the line…

Maybe that restriction should be lifted for compatibility with readdlm (which is leaving the stdlib, seems though will be a transparent change to users):

> <https://github.com/JuliaLang/julia/issues/44663>
>
> Can we move DelimitedFiles to an external package and also perhaps remove it fro…m the system image?
> 
> Now that we are considering moving sparse out of the system image, I thought I would ask that about DelimitedFiles as well. https://github.com/JuliaLang/julia/pull/44247#issuecomment-1071226278
> 
> Is Dates.jl worth considering moving out? Any others?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 15, 2022, 4:08pm UTC](https://discourse.julialang.org/t/reading-a-txt-file-with-two-different-delimiters/81087/7 "2022-05-15T16:08:05Z")

</div>

See also [this related post and its solution](https://discourse.julialang.org/t/reading-data-text-files-delimited-with-both-spaces-tabs/64851/4).

Adapted here for reference:

```julia
using DelimitedFiles, DataFrames

input2="""
6
0
0 0 1; 4
0 0 2; 5
0 0 3; 6
"""
io = IOBuffer(input2)
data = readdlm(IOBuffer(replace(read(io, String), ";" => " ")), skipstart=2) # trick by @GunnarFarneback
df = DataFrame(data, :auto)

```
