-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_data.py
32 lines (22 loc) · 866 Bytes
/
load_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
import json
import sys
import dateutil.parser
if len(sys.argv) != 2:
print('\nUsage: load_data.py <filename>')
print('e.g. python load_data.py test.txt')
exit(1)
filename = sys.argv[1]
print('\nLoading data from {}...'.format(filename))
# Load data as JSON
with open(filename,'r') as file:
data = json.load(file)
print('\nFound {} articles in total'.format(len(data['articles'])))
# Access information within the data - showing first 5 for demo purposes
print('\nDisplaying information on the first 5 articles...')
for article in data['articles'][:5]:
published_datetime = dateutil.parser.parse(article['publishedAt'])
print('\nSource: ', article['source']['name'])
print('Title: ', article['title'])
print('Written by: ', article['author'])
print('Published at:', published_datetime)
print('URL: ', article['url'])