-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea.py
executable file
·80 lines (59 loc) · 1.98 KB
/
area.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/home/sgrosu/anaconda2/bin/python
# -*- coding: utf-8 -*-
"""
In this problem set you work with cities infobox data, audit it, come up with a
cleaning idea and then clean it up.
Since in the previous quiz you made a decision on which value to keep for the
"areaLand" field, you now know what has to be done.
Finish the function fix_area(). It will receive a string as an input, and it
has to return a float representing the value of the area or None.
You have to change the function fix_area. You can use extra functions if you
like, but changes to process_file will not be taken into account.
The rest of the code is just an example on how this function can be used.
"""
import codecs
import csv
import json
import pprint
CITIES = 'cities.csv'
def fix_area(area):
try:
ar_fl = float(area)
area = ar_fl
except:
if area.startswith('{'):
ar_list = area.strip('{').strip('}').split('|')
ar = max(ar_list,key=len)
area = float(ar)
else:
area = None
return area
def process_file(filename):
# CHANGES TO THIS FUNCTION WILL BE IGNORED WHEN YOU SUBMIT THE EXERCISE
data = []
with open(filename, "r") as f:
reader = csv.DictReader(f)
#skipping the extra metadata
for i in range(3):
l = reader.next()
# processing file
for line in reader:
# calling your function to fix the area value
if "areaLand" in line:
line["areaLand"] = fix_area(line["areaLand"])
data.append(line)
return data[]
'''
def test():
data = process_file(CITIES)
print "Printing three example results:"
for n in range(5,8):
pprint.pprint(data[n]["areaLand"])
assert data[3]["areaLand"] == None
assert data[8]["areaLand"] == 55166700.0
assert data[20]["areaLand"] == 14581600.0
assert data[33]["areaLand"] == 20564500.0
if __name__ == "__main__":
test()
'''
print process_file(CITIES)