内閣府の令和2年版高齢社会白書のデータからJSON 形式のデータを作成する方法です。
HTML と PDF でデータが提供されています。
データの取得
wget https://www8.cao.go.jp/kourei/whitepaper/w-2020/zenbun/pdf/1s1s_04.pdf
テキストに変換
pdftotext -layout 1s1s_04.pdf - > pref.txt
エディター で pref.txt を編集して次のようにします。
pdf.txt
北海道 5,250 1,673 31.9 42.8 10.9
青森県 1,246 415 33.3 46.8 13.5
岩手県 1,227 406 33.1 43.2 10.1
宮城県 2,306 652 28.3 40.3 12.0
秋田県 966 359 37.2 50.1 12.9
山形県 1,078 360 33.4 43.0 9.6
福島県 1,846 582 31.5 44.2 12.7
茨城県 2,860 843 29.5 40.0 10.5
栃木県 1,934 554 28.6 37.3 8.7
群馬県 1,942 580 29.8 39.4 9.6
埼玉県 7,350 1,961 26.7 35.8 9.1
千葉県 6,259 1,743 27.9 36.4 8.5
東京都 13,921 3,209 23.1 30.7 7.6
神奈川県 9,198 2,329 25.3 35.2 9.9
新潟県 2,223 720 32.4 40.9 8.5
富山県 1,044 337 32.3 40.3 8.0
石川県 1,138 337 29.6 37.2 7.6
福井県 768 235 30.6 38.5 7.9
山梨県 811 250 30.8 43.0 12.2
長野県 2,049 653 31.9 41.7 9.8
岐阜県 1,987 599 30.1 38.7 8.6
静岡県 3,644 1,089 29.9 38.9 9.0
愛知県 7,552 1,892 25.1 33.1 8.0
三重県 1,781 530 29.7 38.3 8.6
滋賀県 1,414 368 26.0 34.3 8.3
京都府 2,583 753 29.1 37.8 8.7
大阪府 8,809 2,434 27.6 36.2 8.6
兵庫県 5,466 1,591 29.1 38.9 9.8
奈良県 1,330 417 31.3 41.1 9.8
和歌山県 925 306 33.1 39.8 6.7
鳥取県 556 178 32.1 38.7 6.6
島根県 674 231 34.3 39.5 5.2
岡山県 1,890 573 30.3 36.0 5.7
広島県 2,804 823 29.3 35.2 5.9
山口県 1,358 466 34.3 39.7 5.4
徳島県 728 245 33.6 41.5 7.9
香川県 956 305 31.8 38.3 6.5
愛媛県 1,339 442 33.0 41.5 8.5
高知県 698 246 35.2 42.7 7.5
福岡県 5,104 1,425 27.9 35.2 7.3
佐賀県 815 246 30.3 37.0 6.7
長崎県 1,327 433 32.7 40.6 7.9
熊本県 1,748 543 31.1 37.1 6.0
大分県 1,135 373 32.9 39.3 6.4
宮崎県 1,073 346 32.3 40.0 7.7
鹿児島県 1,602 512 32.0 40.8 8.8
沖縄県 1,453 322 22.2 31.4 9.2
JSON に変換
./population_gen.js pref.txt pref.json
population_gen.js
#! /usr/bin/node
// ---------------------------------------------------------------
// population_gen.js
//
// Jun/01/2021
// ---------------------------------------------------------------
'use strict'
var fs = require("fs")
// ---------------------------------------------------------------
function text_read_proc(file_in)
{
var dict_aa = new Object ()
const data = fs.readFileSync (file_in,'utf8')
const lines_in = ("" + data).split ("\n")
lines_in.forEach (function(line)
{
const separator_string = /\s+/
const rr = line.split (separator_string)
const pref = rr[1]
// if (1< pref.length)
if (pref)
{
const number = rr[3]
const removed = number.replace(/,/g, '')
const pp = parseInt(removed,10) * 1000
dict_aa[pref] = {"over_65": pp}
console.log(pref)
}
})
return dict_aa
}
// ---------------------------------------------------------------
function json_write_proc(file_json,dict_aa)
{
const json_str = JSON.stringify(dict_aa)
fs.writeFile (file_json,json_str,function (err)
{
if (err) {
console.error ("Error on write: " + err)
}
else {
console.log("File written.")
console.error ("*** 終了 ***")
}
})
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
//
const file_in=process.argv[2]
const file_json=process.argv[3]
console.log (file_in)
if (fs.existsSync(file_in))
{
const dict_aa = text_read_proc(file_in)
json_write_proc(file_json,dict_aa)
}
else
{
console.error ("*** error *** " + file_in + " doesn't exist. ***")
}
// ---------------------------------------------------------------
次のような JSON が作成できます。
pref.json
{
"北海道": {
"over_65": 1673000
},
"青森県": {
"over_65": 415000
},
"岩手県": {
"over_65": 406000
},
"宮城県": {
"over_65": 652000
},
"秋田県": {
"over_65": 359000
},
"山形県": {
"over_65": 360000
},
"福島県": {
"over_65": 582000
},
"茨城県": {
"over_65": 843000
},
"栃木県": {
"over_65": 554000
},
"群馬県": {
"over_65": 580000
},
(省略)
↧