0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JSONデータを日付毎に保存(Python)

Posted at

概要

JSONデータに指定された日付毎にファイルを保存するツールを作成したいと思います。

JSONデータ

下記のデータを作成して保存します。
文字コードはS-JISで改行コードは「\n」で保存します。

#################
cur20200101.json
#################
{"name":"田中","age":"46","gender":"1","reg_date":"2020/01/01 15:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/01/01 16:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/02/01 17:48:45"}
{"name":"田中","age":"46","gender":"1","reg_date":"2020/02/02 22:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/03/02 23:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/03/02 23:48:45"}

ソース

次のソースを保存します。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

path = "./"
cur_path = os.getcwd()

for file in os.listdir(path):
    if file != 'test.py' and file != 'data':
        with open(file) as f:
            for line in f:
                content = line.split(',')
                for item in content:
                    if(item.find('reg_date')>0):
                        tmp = item.split('":"')
                        dd = tmp[1][0:10].replace("/","")
                        if(file.find('cur') == 0):
                            pre = "cur"
                        elif(file.find('xxx') == 0):
                            pre = "xxx"
                        else:
                            pre = "none"
                        with open(cur_path + "/data/" + pre + dd + ".json", 'a') as writer:
                            writer.write(line)

実行

次のコマンドから実行します。
JSONデータが日付毎にファイルが分かれて保存されていることが確認できます。

# 現在ディレクトリ
pi@raspberrypi:~/work/json $ ls
cur20200101.json  data  test.py

#元のデータを確認
pi@raspberrypi:~/work/json $ cat cur20200101.json
{"name":"田中","age":"46","gender":"1","reg_date":"2020/01/01 15:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/01/01 16:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/02/01 17:48:45"}
{"name":"田中","age":"46","gender":"1","reg_date":"2020/02/02 22:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/03/02 23:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/03/02 23:48:45"}

#ソースを実行
pi@raspberrypi:~/work/json $ python test.py

#dataフォルダにファイルを確認
pi@raspberrypi:~/work/json $ ls -alh data
合計 24K
drwxr-xr-x 2 pi pi 4.0K  6月 15 11:34 .
drwxr-xr-x 3 pi pi 4.0K  6月 15 11:34 ..
-rw-r--r-- 1 pi pi  150  6月 15 11:34 cur20200101.json
-rw-r--r-- 1 pi pi   75  6月 15 11:34 cur20200201.json
-rw-r--r-- 1 pi pi   75  6月 15 11:34 cur20200202.json
-rw-r--r-- 1 pi pi  150  6月 15 11:34 cur20200302.json

#保存されているファイルの内容を元のファイルと確認
pi@raspberrypi:~/work/json $ cat data/cur20200101.json
{"name":"田中","age":"46","gender":"1","reg_date":"2020/01/01 15:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/01/01 16:18:12"}
pi@raspberrypi:~/work/json $ cat data/cur20200201.json
{"name":"水島","age":"52","gender":"1","reg_date":"2020/02/01 17:48:45"}
pi@raspberrypi:~/work/json $ cat data/cur20200202.json
{"name":"田中","age":"46","gender":"1","reg_date":"2020/02/02 22:33:25"}
pi@raspberrypi:~/work/json $ cat data/cur20200302.json
{"name":"鈴木","age":"32","gender":"1","reg_date":"2020/03/02 23:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/03/02 23:48:45"}
pi@raspberrypi:~/work/json $

終わりに

実は大量のデータをエクセルから作成した後、VBAでJSON形式でデータを作成しましたが、
VBAからファイル保存処理が面倒だったので、こちらのPythonでJSON形式のチェック及びファイルの保存する処理を作成してみました。
次回はPythonでJSONファイルを日付フォルダに格納した後ファイルも「gz」に圧縮するソースを作成したいと思います。
今日は以上です!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?