Skip to content

Instantly share code, notes, and snippets.

@kwcooper
Last active September 27, 2023 12:58
Show Gist options
  • Save kwcooper/a21ba58272d3cdf26310cc02ee4b168f to your computer and use it in GitHub Desktop.
Save kwcooper/a21ba58272d3cdf26310cc02ee4b168f to your computer and use it in GitHub Desktop.
To clean and convert a whatsapp txt file export to a CSV file
# To clean and convert a whatsapp txt file export to a CSV file
import pandas as pd
# read file by lines
file_path = "whatsapp.txt"
f = open(file_path, 'r')
data = f.readlines()
f.close()
# sanity stats
print('num lines: %s' %(len(data)))
# parse text and create list of lists structure
# remove first whatsapp info message
dataset = data[1:]
cleaned_data = []
for line in dataset:
# grab the info and cut it out
date = line.split(",")[0]
line2 = line[len(date):]
time = line2.split("-")[0][2:]
line3 = line2[len(time):]
name = line3.split(":")[0][4:]
line4 = line3[len(name):]
message = line4[6:-1] # strip newline charactor
#print(date, time, name, message)
cleaned_data.append([date, time, name, message])
# Create the DataFrame
df = pd.DataFrame(cleaned_data, columns = ['Date', 'Time', 'Name', 'Message'])
# check formatting
if 0:
print(df.head())
print(df.tail())
# Save it!
df.to_csv(r'converted_messages.csv', index=False)
@stafa-san
Copy link

This is really great. Please can you assist me with a version of this in JavaScript ?

@kwcooper
Copy link
Author

Thank you! I believe there are online converters now that should do the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment