tsukuba-kdbのcsv0.pyの内容

github.com

import csv
import json

output = []
headline = ""
with open("kdb_202104042.csv", encoding="utf-8") as fp:
    reader = csv.reader(fp)

    for line in reader:
        # B列削除
        line.pop(1)

        # M-Q列削除
        for i in range(5):
            line.pop(11)

        isEmpty = False
        isHead = False

        # A列が科目番号の場合、スキップ
        if line[0] == "科目番号":
            continue

        # A列が空白 isEmptyがTrue
        if line[0] == "":
            isEmpty = True

        # A列が空白じゃなく、C列が空白 isHeadがTrue
        if not isEmpty and line[1] == "":
            isHead = True

        # isHeadがTrueの場合、A列をheadlineにコピー
        if isHead:
            headline = line[0]
            print(line[0])

        # A列かC列が空白の場合、スキップ
        if isEmpty or isHead:
            continue

        # T列が空白なら削除
        if line[13] == "":
            line.pop(13)
        # S列が空白なら削除
        if line[12] == "":
            line.pop(12)

        line.append(headline)
        output.append(line)

with open("kdb.json", "w", encoding="utf-8") as fp:
    json.dump(output, fp, indent="\t", ensure_ascii=False)