# Parsing WhatsApp text chat

**URL:** <https://discourse.julialang.org/t/parsing-whatsapp-text-chat/60427>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [May 2, 2021, 6:19pm UTC](https://discourse.julialang.org/t/parsing-whatsapp-text-chat/60427 "2021-05-02T18:19:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [May 2, 2021, 6:19pm UTC](https://discourse.julialang.org/t/parsing-whatsapp-text-chat/60427/1 "2021-05-02T18:19:02Z")

</div>

Hello,  
I am trying to parse a whatsapp text chat as a `DataFrame` . What I need is to parse this format:

`06/18/2021 20:48 - +36 41 9989-8989: Hello, how are you?` \<= all in one column  
or  
`06/18/2021 20:48 - Paul Faben: Hello, how are you?` \<= all in one column

to get this table:

`Date | Time | Number | Text` \<= 4 columns

Any help would be great, as I am new to Julia…  
Thanks

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 2, 2021, 6:51pm UTC](https://discourse.julialang.org/t/parsing-whatsapp-text-chat/60427/2 "2021-05-02T18:51:08Z")

</div>

I am sure it’s possible to implement a more efficient version of this by writing a custom parser, but to implement a simple version, you can just use a regex together with the `Dates` stdlib:

```julia
using DataFrames, Dates

function read_chats(str)
    df = DataFrame(date=Date[], time=Time[], number=String[], text=String[])
    for row in eachmatch(r"^(?<date>.+?) (?<time>.+?) \- (?<number>.*?)\: (?<text>.*)$"m, str)
        date = parse(Date, row["date"], dateformat"mm/dd/yyyy")
        time = parse(Time, row["time"])
        push!(df, (; date, time, number=row["number"], text=row["text"]))
    end
    return df
end

```

`r"^(?<date>.+?) (?<time>.+?) \- (?<number>.*?)\: (?<text>.*)$"m` is a regex describing the format your lines are in, where each part you are interested in is a named group. You can use `parse` to then parse the dates and times to `Date` and `Time` objects.

Reading in two rows:

```julia
julia> read_chats("""
       06/18/2021 20:48 - +36 41 9989-8989: Hello, how are you?
       06/18/2021 20:48 - Paul Faben: Hello, how are you?""")
2×4 DataFrame
 Row │ date time number text                
     │ Date Time String String              
─────┼─────────────────────────────────────────────────────────────
   1 │ 2021-06-18 20:48:00 +36 41 9989-8989 Hello, how are you?
   2 │ 2021-06-18 20:48:00 Paul Faben Hello, how are you?

```

To read from a file, you can just read it into a string using `read(filename, String)`.

---

<div class="post-metadata">

**Author:** ![statspy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statspy/32/26630_2.png) [@statspy](https://discourse.julialang.org/u/statspy)\
**Post date:** [May 2, 2021, 6:55pm UTC](https://discourse.julialang.org/t/parsing-whatsapp-text-chat/60427/3 "2021-05-02T18:55:27Z")

</div>

Awesome! Thank you very much @simeonschaub
