# Parsing a bespoke file format

**URL:** <https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463>\
**Category:** General Usage\
**Tags:** question, csv\
**Created:** [September 20, 2021, 1:41pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463 "2021-09-20T13:41:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [September 20, 2021, 1:41pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/1 "2021-09-20T13:41:19Z")

</div>

I have a text file that has the following structure

```julia
begin"head"
    <head1 content>
end"head"
begin"body"
    <body1 content>
end"body"
begin"foot"
    <foot1 content>
end"foot"
begin"head"
    <head2 content>
end"head"
begin"body"
    <body2 content>
end"body"
begin"foot"
    <foot2 content>
end"foot"
...

```

Such files are collections of multiple datasets, where one dataset is composed of one `head`, `body` and `foot`.  
Obviously, such files should be handled in a concurrent manner, each dataset independently.

Is there a way to tell `CSV` to split the file in chunks at all the `begin"head"` ?

---

<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:** [September 20, 2021, 7:18pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/2 "2021-09-20T19:18:10Z")

</div>

Not sure why CSV.jl should read such type of file but, why not considering writing your own file parser?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 20, 2021, 8:39pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/3 "2021-09-20T20:39:57Z")

</div>

Agree, this file looks very non-CSV in format. Unclear if data is even tabular.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [September 21, 2021, 7:34am UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/4 "2021-09-21T07:34:29Z")

</div>

@rafael.guerra @StefanKarpinski, yeah sorry, I hid the only part of the file that _looks_ like a CSV.  
Basically, anything inside a `begin ... end` is composed of items separated with `"` (yes somebody thought of that character for a separator…).

My idea was to use `CSV` with the transposition option and get rid of all the `begin...end`. But as each line has a different amount of information (there is no consistency in columns counts) it may not be suited for such files indeed.

I just figured `CSV` was well developed now and might be of help for this.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 21, 2021, 1:23pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/5 "2021-09-21T13:23:41Z")

</div>

I think this is different enough from CSV that trying to get a CSV reader to process it will be much more pain than help. I would write a loop that processes the file format line by line using regular expressions and split to extract the data.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [September 21, 2021, 1:36pm UTC](https://discourse.julialang.org/t/parsing-a-bespoke-file-format/68463/6 "2021-09-21T13:36:19Z")

</div>

All right, thanks for the advice !
