-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_whatsapp_data.py
executable file
·34 lines (27 loc) · 1.27 KB
/
create_whatsapp_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from myimports import *
def create_whatsapp_data(chat_file, name_person_1, name_person_2):
person1_datastore = list(dict())
person2_datastore = list(dict())
pattern = re.compile("[^\w']")
def strip_line(line):
stripped_line = pattern.sub(' ', line).strip()
datetime_str = stripped_line[0:17]
datetime_obj = datetime.strptime(datetime_str, '%d %m %y %H %M %S')
return datetime_obj, stripped_line
with open(chat_file, "rb") as input_file:
for line in input_file.readlines():
if name_person_1 in line:
datetime_obj, stripped_line = strip_line(line)
message = stripped_line[34:]
person1_datastore.append(
{'Datetime': str(datetime_obj), 'Message': message})
if name_person_2 in line:
datetime_obj, stripped_line = strip_line(line)
message = stripped_line[35:]
person2_datastore.append(
{'Datetime': str(datetime_obj), 'Message': message})
input_file.close()
with open(name_person_1+'_datastore.json', 'w') as outfile:
json.dump(person1_datastore, outfile)
with open(name_person_2+'_datastore.json', 'w') as outfile:
json.dump(person2_datastore, outfile)