Skip to content

Instantly share code, notes, and snippets.

@potass13
Created December 16, 2014 20:25
Show Gist options
  • Save potass13/e0b6aa337d80262256c5 to your computer and use it in GitHub Desktop.
Save potass13/e0b6aa337d80262256c5 to your computer and use it in GitHub Desktop.
Convert xls-files of population statistics for Takayama to a csv-file
# -*- cording: utf-8 -*-
#
# filename: tkym_stat.py
#
import os
import sys
import wget
import xlrd
if len(sys.argv) != 1:
if not os.path.exists(sys.argv[1]):
os.mkdir(sys.argv[1])
os.chdir(sys.argv[1])
path = "http://www.city.takayama.lg.jp/shimin/documents/"
print("year,month,male_pop,female_pop,households", end = "")
for h_year in range(13, 27):
url = path
if h_year in range(13, 24):
url = url + "H%2djinkou" %(h_year)
if h_year == 14:
url = url + "_000"
elif h_year == 23:
url = url + "_009"
else:
url = url + "H%2d_jinkou" %(h_year)
if h_year == 24:
url = url + "_009"
elif h_year == 26:
url = url + "_003"
url = url + ".xls"
os.rename(wget.download(url), "H%02d_tkym_stat.xls" %(h_year))
xls_book = xlrd.open_workbook("H%02d_tkym_stat.xls" %(h_year))
sheet1 = xls_book.sheet_by_index(0)
for row in range(3, 15):
if row in range(3, 12):
print("\n%04d" %(1988 + h_year), end = "")
else:
print("\n%04d" %(1988 + h_year + 1), end = "")
for col in [0, 1, 2, 4]:
if col == 0:
if h_year == 16 and row == 13:
print(",02", end = "")
else:
print(",%02d" %(sheet1.cell(row, col).value), end = "")
else:
print("," + str(sheet1.cell(row, col).value), end = "")
sys.stderr.write("H%2d is Finished!\n" %(h_year))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment