This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pedromlsreis/dates
Get dates from a given chatfile.html
- Loading branch information
Showing
1 changed file
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import csv\n", | ||
"from bs4 import BeautifulSoup\n", | ||
"import re\n", | ||
"from datetime import datetime\n", | ||
"import pandas as pd\n", | ||
"\n", | ||
"\n", | ||
"# import matplotlib.pyplot as plt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_dates(filename):\n", | ||
" #Facebook Messenger date format: Jun 8, 2013, 10:50 PM\n", | ||
" date_pattern = re.compile(r'[A-Z]{1}[a-z]{2}\\s\\d+[,]\\s\\d{4}[,]\\s\\d{1,2}[:]\\d{2}\\s[AM|PM]')\n", | ||
" with open(filename, mode='r', encoding='UTF-8', errors='ignore') as fp:\n", | ||
" soup = BeautifulSoup(fp, \"lxml\", from_encoding='utf-8')\n", | ||
" soup.find('head').decompose() #delete the <head>, useless for this project\n", | ||
" soup.find('div', id=\"bluebarRoot\").decompose() #delete the blue header\n", | ||
" dates = soup.find_all(text=date_pattern)\n", | ||
" csvfile = csv.writer(open(\"chatdata.csv\", \"w\", encoding='UTF-8'))\n", | ||
" csvfile.writerow([\"date\"])\n", | ||
" for date in dates:\n", | ||
" data = datetime.strptime(date,'%b %d, %Y, %I:%M %p')\n", | ||
" csvfile.writerow([data])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"get_dates(r\"\\Path\\to\\File\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>date</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>2018-12-22 14:23:00</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>2018-12-21 02:35:00</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>2018-12-21 02:35:00</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>2018-12-21 02:35:00</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td>2018-12-21 02:35:00</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" date\n", | ||
"0 2018-12-22 14:23:00\n", | ||
"1 2018-12-21 02:35:00\n", | ||
"2 2018-12-21 02:35:00\n", | ||
"3 2018-12-21 02:35:00\n", | ||
"4 2018-12-21 02:35:00" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"df = pd.read_csv('chatdata.csv')\n", | ||
"df.head()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |