I'm trying to parse a WhatsApp chat log and organize it into a DataFrame. I've managed to separate it into Date, Time, Name, and the Message. However, there are some newline messages that are continuations of the previous message and are in the "Date" column when I create the DataFrame. I want them to be appended to the previous message cell.
This is what the raw .txt file looks like (I made up some text to hide messages):
11/28/17, 10:00 AM - Bob: Lorem ipsum dolor sit amet
11/28/17, 10:00 AM - Marley: Yes!
11/28/17, 10:00 AM - Marley: consectetur adipiscing elit
11/28/17, 10:00 AM - Bob: Barely dude. BARELY
11/28/17, 10:01 AM - Bob: sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
11/28/17, 10:14 AM - Marley: Ut enim ad minim veniam
11/28/17, 10:20 AM - Marley: quis nostrud exercitation
Duis aute irure dolor in
11/28/17, 10:31 AM - Bob: Hahaha proud
11/28/17, 10:31 AM - Bob: Can't imagine
As you can see, the message at 11/28/17, 10:20 AM is two lines. I want to append that extra message line to be with the message in the previous line. When I convert to a DataFrame, everything would belong in the correct column. Here's my code so far:
import pandas as pd
with open('whatsapp.txt', encoding="utf8") as f:
mylist = list(f)
df = pd.DataFrame(mylist)
df = df[0].str.split(r'[,-]', 2, expand=True)
df = df.rename(columns={0:"Date",1:"Time",2:"Name"})
df = df.replace('\n','', regex=True)
df[['Name','Message']] = df['Name'].str.split(':',1,expand=True)
My logic is before creating the DataFrame to find the list elements that do NOT start with '\d{1,2}/', then append those to the end of the previous element. Any ideas on how I can do this?
