-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountains.py
37 lines (31 loc) · 1.27 KB
/
mountains.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
35
36
37
#!/usr/bin/env python
#
# A simple command-line tool for processing mountain files
import argparse
import csv
import requests
from datetime import datetime
timestamp = datetime.now()
def unicode_csv_dictreader(unicode_csv_data, **kwargs):
"""csv.py doesn't do Unicode, so encode as UTF-8"""
csv_dictreader = csv.DictReader(unicode_csv_data, **kwargs)
for row in csv_dictreader:
yield dict((key, unicode(value, 'utf-8'))
for key, value in row.iteritems())
if __name__ == '__main__':
"""Download and parse mountain data files for name and altitude."""
parser = argparse.ArgumentParser(description="Parse mountain data files")
parser.add_argument("URL", help="A URL to a mountain data file.")
args = parser.parse_args()
print timestamp.strftime(u"%Y-%m-%d %H:%M:%S (%A)\n")
# For potentially large downloads, stream the response, and iterate
# over the lines.
csvdata = requests.get(args.URL, stream=True).iter_lines()
# Use a Unicode wrapper around DictReader
mcsv = unicode_csv_dictreader(csvdata)
for row in mcsv:
name = row['Name']
altitude = row['Altitude (m)']
if altitude == 'null':
altitude = 'unknown'
print u"%s has an altitude of %s meters." % (name, altitude)