Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

WhatsParser-0.5.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

WhatsParser-0.5.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file WhatsParser-0.5.0.tar.gz.

File metadata

  • Download URL: WhatsParser-0.5.0.tar.gz
  • Upload date:
  • Size: 4.3 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

Hashes for WhatsParser-0.5.0.tar.gz
Algorithm Hash digest
SHA256 769bfa0b700473a1fcc4dacb1d2c6a6f7b763973b2e8bf63e01257939584e9f2
MD5 5c332d8e0fec0aac99d6b77e7e0a8d42
BLAKE2b-256 2ee2cc7a037d0834c9296ee89531b4d69cc4d7708a4308ac67ab0740983676be

See more details on using hashes here.

File details

Details for the file WhatsParser-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: WhatsParser-0.5.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

Hashes for WhatsParser-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56fba97ebc862b0aa8fc4ed8144ccee1342c6ebf0cd5fac64e221f7bd69d9931
MD5 81bc93fb3a51adf65578a19f2907b1ae
BLAKE2b-256 8c54443ddadc9c6f15601a125164012a631009914189ebceec2ce11c1fb561c9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page