aboutsummaryrefslogtreecommitdiffstats
path: root/administrative-codes/update
diff options
context:
space:
mode:
Diffstat (limited to 'administrative-codes/update')
-rwxr-xr-xadministrative-codes/update21
1 files changed, 13 insertions, 8 deletions
diff --git a/administrative-codes/update b/administrative-codes/update
index 855f73b..fa6a6da 100755
--- a/administrative-codes/update
+++ b/administrative-codes/update
@@ -18,6 +18,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#----------------------------------------------------------------------
+# pylint: disable=missing-module-docstring
+
import re
import sys
import csv
@@ -30,13 +32,15 @@ import xlrd
#
# Unfortunately SCB doesn't provide a CSV, so we download their xls file and produce our own.
# https://www.scb.se/hitta-statistik/regional-statistik-och-kartor/regionala-indelningar/lan-och-kommuner/lan-och-kommuner-i-kodnummerordning/
-r = requests.get('https://www.scb.se/contentassets/7a89e48960f741e08918e489ea36354a/kommunlankod_2024.xls')
+r = requests.get('https://www.scb.se/contentassets/7a89e48960f741e08918e489ea36354a/'
+ 'kommunlankod_2024.xls',
+ timeout=30)
r.raise_for_status()
if 'content-type' not in r.headers:
- raise Exception('Missing Content-Type from response headers')
-if r.headers['content-type'] not in ['application/vnd.ms-excel', 'application/octet-stream']:
- raise Exception(f"Unsupported Content-Type: {r.headers['content-type']}")
+ raise RuntimeError('Missing Content-Type from response headers')
+if r.headers['content-type'] not in ('application/vnd.ms-excel', 'application/octet-stream'):
+ raise RuntimeError(f"Unsupported Content-Type: {r.headers['content-type']}")
xls = xlrd.open_workbook(file_contents=r.content)
sheets = xls.sheet_names()
@@ -71,15 +75,16 @@ for i in range(sheet.nrows):
counties.append(row)
basedir = Path(sys.argv[0]).parent
-def writeCSV(filename, data):
+def writeCSV(filename, data): # pylint: disable=invalid-name
+ """Write CSV file."""
fieldnames = ['Code', 'Name']
path = basedir.joinpath(filename).with_suffix('.csv')
- with path.open(mode='w', newline='') as fp:
+ with path.open(mode='w', newline='', encoding='utf-8') as fp:
writer = csv.DictWriter(fp, fieldnames=fieldnames, delimiter='\t',
quoting=csv.QUOTE_MINIMAL, dialect='unix')
writer.writeheader()
- for row in data:
- writer.writerow(row)
+ for row2 in data:
+ writer.writerow(row2)
print(f'Wrote {len(data)} rows in {path}', file=sys.stderr)
writeCSV('counties', counties)