Created
March 18, 2014 03:51
-
-
Save jpmckinney/9613246 to your computer and use it in GitHub Desktop.
Builds the list at https://docs.google.com/a/opennorth.ca/spreadsheet/ccc?key=0AtzgYYy0ZABtdEFYY1dnQlFDdDR1Zk40RHlTZzNwZUE&usp=drive_web#gid=0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'open-uri' | |
data_catalogs = {} | |
rows = CSV.parse(open('https://raw.github.com/opencivicdata/ocd-division-ids/master/identifiers/country-ca/ca_municipal_subdivisions-data_catalog.csv').read) | |
rows.shift | |
rows.each do |id,data_catalog| | |
data_catalogs[id[/[^:]+\z/]] = data_catalog | |
end | |
# @see http://www12.statcan.gc.ca/census-recensement/2011/dp-pd/hlt-fst/pd-pl/index-eng.cfm | |
file = open("http://www12.statcan.gc.ca/census-recensement/2011/dp-pd/hlt-fst/pd-pl/FullFile.cfm?T=301&LANG=Eng&OFT=CSV&OFN=98-310-XWE2011002-301.CSV") | |
# The CSV has an extra header row. | |
file.gets | |
# The CSV is in ISO-8859-1. | |
text = file.read.force_encoding("ISO-8859-1").encode("UTF-8") | |
puts CSV.generate_line(['Geographic code', 'Geographic name', 'Geographic type', 'Population, 2011', 'Data catalog']) | |
CSV.parse(text, headers: true, skip_blanks: true).each do |row| | |
code = row.fetch("Geographic code") | |
name = row.fetch("Geographic name") | |
type = row.fetch("Geographic type") | |
population = row.fetch("Population, 2011") | |
# Skip "Canada" row. | |
next if code == "01" | |
# Stop before footer. | |
break if code == "Note:" | |
puts CSV.generate_line([code, name, type, population, data_catalogs[code]]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment