-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherry
More file actions
42 lines (33 loc) · 1.2 KB
/
sherry
File metadata and controls
42 lines (33 loc) · 1.2 KB
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
35
36
37
38
39
40
41
42
def users_dict_from_file(user_file):
with open(user_file, 'r') as f:
user_dict_list = json.load(f)
username_dict = {}
for user in user_dict_list:
user_id = user['id']
username = user['name']
username_dict[user_id] = username
return username_dict
# print users
def print_users(users_dict):
for key, value in users_dict.items():
print(key, value)
# load messages
def messages_from_file(message_file):
with open(message_file, 'r') as f:
message_dict_list = json.load(f)
return message_dict_list
# print messages
def print_messages(message_dict_list, username_dict):
for message in message_dict_list:
username = username_dict[message['user']]
timestamp = float(message['ts'])
formatted_time = time.ctime(timestamp)
print(formatted_time, ':')
print(username, ':', message['text'])
# run code
user_file = '/Users/sherry/PycharmProjects/chat/users.json'
message_file = '/Users/sherry/PycharmProjects/chat/2015-10-15.json'
username_dict = users_dict_from_file(user_file)
print_users(username_dict)
messages = messages_from_file(message_file)
print_messages(messages, username_dict)