LoginSignup
0
0

More than 1 year has passed since last update.

JSONデータを日付フォルダ毎に保存(Python)

Last updated at Posted at 2022-06-28

概要

前回日付毎に作成されたJSONファイルをgz形式に圧縮してフォルダに格納するコマンドを作成したいと思います。

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"}

ソース

このソースをtest.pyファイル名に保存します。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import gzip
import shutil

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

for file in os.listdir(path):
    if file != 'test.py' and 'month' in file:
        dd = file.replace('cur','')
        dd = dd.replace('.json','')
        fd = dd
        monthdir = 'month=' + dd[4:6]
        daydir = 'day=' + dd[6:8]

        if(not os.path.exists(cur_path + "/" + monthdir)):
            os.mkdir(cur_path + "/" + monthdir)

        if(not os.path.exists(cur_path + "/" + monthdir + "/" + daydir)):
            os.mkdir(cur_path + "/" + monthdir + "/" + daydir)

        with open(cur_path + "/" + file, 'rb') as f_in:
            with gzip.open(cur_path + "/" + file + ".gz", "wb") as f_out:
                shutil.copyfileobj(f_in, f_out)
                shutil.move(cur_path + "/" + file + ".gz", cur_path + "/" + monthdir + "/" + daydir + "/" + file + ".gz")

実行

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

pi@raspberrypi:~/work $ ls -alh
合計 28K
drwxr-xr-x  2 pi pi 4.0K  6月 28 11:07 .
drwxr-xr-x 23 pi pi 4.0K  6月 28 10:58 ..
-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
-rw-r--r--  1 pi pi  890  6月 28 10:49 test.py
pi@raspberrypi:~/work $ python test.py
pi@raspberrypi:~/work $ ls -alh
合計 40K
drwxr-xr-x  5 pi pi 4.0K  6月 28 11:08  .
drwxr-xr-x 23 pi pi 4.0K  6月 28 10:58  ..
-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
drwxr-xr-x  3 pi pi 4.0K  6月 28 11:08 'month=01'
drwxr-xr-x  4 pi pi 4.0K  6月 28 11:08 'month=02'
drwxr-xr-x  3 pi pi 4.0K  6月 28 11:08 'month=03'
-rw-r--r--  1 pi pi  890  6月 28 10:49  test.py
pi@raspberrypi:~/work $ ls month\=01/day\=01/cur20200101.json.gz 
'month=01/day=01/cur20200101.json.gz'
pi@raspberrypi:~/work $ ls month\=02/day\=01/cur20200201.json.gz 
'month=02/day=01/cur20200201.json.gz'
pi@raspberrypi:~/work $ ls month\=02/day\=02/cur20200202.json.gz 
'month=02/day=02/cur20200202.json.gz'
pi@raspberrypi:~/work $ ls month\=03/day\=02/cur20200302.json.gz 
'month=03/day=02/cur20200302.json.gz'
pi@raspberrypi:~/work $ 

圧縮ファイルの解凍及び内容確認

次のコマンドで圧縮ファイルを解凍した後、内容を確認します。

pi@raspberrypi:~/work $ cd month\=01/day\=01/
pi@raspberrypi:~/work/month=01/day=01 $ ls
cur20200101.json.gz
pi@raspberrypi:~/work/month=01/day=01 $ gunzip cur20200101.json.gz 
pi@raspberrypi:~/work/month=01/day=01 $ ls
cur20200101.json
pi@raspberrypi:~/work/month=01/day=01 $ 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"}
pi@raspberrypi:~/work/month=01/day=01 $ 

終わりに

これでいかがでしょうか。
ファイルの日付を見て対象ディレクトリに圧縮して保存するコマンドです。
まだ初心者のレベルのコードしか書いていませんので、応用して利用してください。
次回は別々作成されたソースを1つのファイルに完成したいと思います。
今日はここまでです。ありがとうございます。

0
0
1

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