LoginSignup
0
0

More than 1 year has passed since last update.

Node.js: 高齢者等の都道府県別接種回数を数える

Last updated at Posted at 2021-06-06

こちらにあるデータを加工して、高齢者等の都道府県別接種回数を数えます。
新型コロナワクチンの接種状況(高齢者等)
「都道府県別接種回数詳細」の prefecture.ndjson をダウンロードします。

sum_up.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  sum_up.js
//
//                      Jun/06/2021
// ---------------------------------------------------------------
'use strict'

const ndjson = require('ndjson')
var fs = require("fs")

const file_in=process.argv[2]
const file_json=process.argv[3]
console.log (file_in)

var first = Array(47)
var second = Array(47)
first.fill(0)
second.fill(0)

fs.createReadStream(file_in)
    .pipe(ndjson.parse())
    .on('data', function(obj) {
    const pref = obj.prefecture
    const index = parseInt(pref,10) - 1
    if (obj.age == "65-")
        {
        if (obj.status == 1)
            {
            first[index] += obj.count
            }
        else if (obj.status == 2)
            {
            second[index] += obj.count
            }
        }
  })

    .on('end', function() {
        var data_out = new Object()
        for (var it in first)
            {
            const jt = parseInt(it) + 1
            const code_pref = ("00" + jt).slice( -2 )
            var unit_aa = new Object()
            unit_aa["first"] = first[it]
            unit_aa["second"] = second[it]
            data_out[code_pref] = unit_aa
            }

        const json_str = JSON.stringify(data_out)
        fs.writeFile (file_json,json_str,function (err)
    {
    if (err) {
        console.error ("Error on write: " + err)
        }
    else {
        console.log("File written.")
        console.error ("*** 終了 ***")
        }
    })  
//      console.log ("num_first = " + first[8])
//      console.log ("num_second = " + second[8])
        console.log("*** end ***")
    })


// ---------------------------------------------------------------

実行コマンド

export NODE_PATH=/usr/lib/node_modules
/sum_up.js prefecture.ndjson korei.json

次のファイルが作成されます。

korei.json
{
  "10": {
    "first": 131155,
    "second": 13156
  },
  "11": {
    "first": 364873,
    "second": 24304
  },
  "12": {
    "first": 327663,
    "second": 24921
  },
  "13": {
    "first": 741621,
    "second": 45711
  },
  "14": {
    "first": 367928,
    "second": 22566
  },
(省略)

参考ページ
Node.js: ndjson の使い方

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0