A parser for whatsapp .txt files
Project description
From WhatsApp chats to pandas dataframe made easy
WhatsParser is a tool for parsing .txt chat files rendered by the WhatsApp messaging App. Is intended to make the shift from WhatsApp data to pandas dataframe as rapid as possible. Reading and parsing the .txt file is done like this:
from whatsparser import WhatsParser
messages = WhatsParser('./chat.txt')
Once the file has been parsed, all messages are stored as dictionaries with three keys: datetime, author and content. Using indexing you can access individual data point:
len(messages) # Get how many messages there are
>> 3590
messages[35] # Get a message
>> {'datetime': datetime.datetime(2017, 9, 15, 19, 10, 2),
'author': 'Agustin Rodriguez',
'content': 'Hi! this is a Whatsapp message'}
The datetime key stores a datetime object, all the others have string as values.
Pandas dataframe
Convert all messages into a pandas DataFrame so you can use your favorite tools for data analysis:
df = messages.to_dataframe() # Returns a pandas dataframe
Looping
WhatsParser also offer the possibility of iterate through the object using various functions. When iterating over messages a copy is made of all messages stored and iteration and changes occurs over this copy. It is possible to change the data store inside messages by assigning the results of the iteration to messages.data.
Filter messages
def find_long_messages(message):
if len(message['content']) > 100:
return True
return False
messages.data = list(filter(find_long_messages, messages))
# Now, messages contains only those messages with a length greater than 100 characters.
List comprehension
from emoji import get_emoji_regexp
def remove_emojis(message):
message['content'] = get_emoji_regexp().sub(r'', message['content'])
return message
messages.data = [remove_emojis(message) for message in messages]
# All messages got their emojis remove from the text
Map function
def remove_emojis(message):
message['content'] = get_emoji_regexp().sub(r'', message['content'])
return message
messages.data = list(map(remove_emojis, messages))
For loop
Iterate over messages.data to make changes on the fly, if no just use messages.
# For changing data
for message in messages.data:
message['content'] = 'NEW CONTENT'
# Without changing the data
for message in messages:
print(message['author'])
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file WhatsParser-0.6.0.tar.gz.
File metadata
- Download URL: WhatsParser-0.6.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4436524423b27bb0ba89890435246f264c405bf611d4847f2978e81a38ae65e
|
|
| MD5 |
35ea418fcacd79e32029fd082635af3f
|
|
| BLAKE2b-256 |
b5846cca53ffd5038574aabc5e95f9de30373aa20c2323efe3791fc5e428248d
|
File details
Details for the file WhatsParser-0.6.0-py3-none-any.whl.
File metadata
- Download URL: WhatsParser-0.6.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69abddac26695136b3d2bdbaec28d54ea2b17e2a30e78206c7eb67830a1c941c
|
|
| MD5 |
8f983b0a3424149cee7040c94b32047f
|
|
| BLAKE2b-256 |
08955f378ede475a12a836b4e85ab42566223e72356865fb5a429a15f93a0d13
|