-
Notifications
You must be signed in to change notification settings - Fork 0
/
cososloan.py
61 lines (47 loc) · 1.45 KB
/
cososloan.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
import urllib
import zipfile
# the url i want to get
URL = "http://tracer.sos.colorado.gov/PublicSite/Docs/BulkDataDownloads/2014_LoanData.csv.zip"
#the web connection
web_cnx = urllib.urlopen(URL).read()
# save the file
with open("2014_LoanData.csv.zip", "w") as output:
output.write(web_cnx)
output.close()
#unzip the file
zip = zipfile.ZipFile("2014_LoanData.csv.zip", 'r')
zip.extractall()
zip.close()
# change the encoding
sourceEncoding = "iso-8859-1"
targetEncoding = "utf-8"
source = open("2014_LoanData.csv")
target = open("2014_LoanData.csv", "w")
target.write(unicode(source.read(), sourceEncoding).encode(targetEncoding))
# second URL to get
URL = "http://tracer.sos.colorado.gov/PublicSite/Docs/BulkDataDownloads/2013_LoanData.csv.zip"
#the web connection
web_cnx = urllib.urlopen(URL).read()
# save the file
with open("2013_LoanData.csv.zip", "w") as output:
output.write(web_cnx)
output.close()
#unzip the file
zip = zipfile.ZipFile("2013_LoanData.csv.zip", 'r')
zip.extractall()
zip.close()
# strips the first line out of the 2013 file and creates a new file
f = open('2013_LoanData.csv', 'r')
n = open('2013Loan.csv', 'w')
for line in f:
if line.startswith('"CO_ID",'):
continue
n.write(line)
f.close()
n.close()
# change the encoding
sourceEncoding = "iso-8859-1"
targetEncoding = "utf-8"
source = open("2013Loan.csv")
target = open("2014_LoanData.csv", "a")
target.write(unicode(source.read(), sourceEncoding).encode(targetEncoding))