LoginSignup
0
0

More than 1 year has passed since last update.

Linuxにてファイルの中にある日付データ置換(python)

Posted at

概要

今回はテキストファイルの中に指定されている日付を一括で置換するコードを紹介いたします。

ファイルのディレクトリ情報

テキストファイルが格納されているディレクトリは以下の通りです。
※ファイル名の形式が「ファイル名年月日.json」に指定されています。

/ 
|
-----month=01
     |
     ----DAY=01
         |
         ----XX20250101.json
     ----DAY=02
     ----.
     ----.
     ----DAY=31
-----month=02
-----month=03
-----.
-----.
-----.
-----month=12

ファイルの内容

次のファイルのように登録日時項目に指定されているデータから日付のみ変更になります。

#################
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/01/01 17:48:45"}

ソース

次のコードをコピーして適当なファイル名に保存しましょう。

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

path = "./"

cur_path = os.getcwd()

for submonthdir in os.listdir(path):
    if submonthdir != 'test.py':
        for subdaydir in os.listdir(cur_path + "/" + str(submonthdir)):
            for filename in os.listdir(cur_path + "/" + submonthdir + "/" + subdaydir):
                dd = filename.replace('cur','')
                dd = dd.replace('.json','')
                fn = dd #ファイルの日付部分を取得
                dd = dd[0:4] + "/" + dd[4:6] + "/" + dd[6:8] #日付形式に編集
                dst = "2025" + "/" + dd[5:7] + "/" + dd[8:10] #日付形式に編集
                print(dd)
                print(dst)

                #ファイルの内容を変数に文字列として設定
                filedir = cur_path + "/" + submonthdir + "/" + subdaydir + "/" + filename
                with open(filedir) as reader:
                    content = reader.read()

                #日付を置換
                content = content.replace(dd,dst)

                #既存ファイルを削除
                os.remove(filedir)

                #ファイルの編集内容を新しいファイル名に保存
                with open(cur_path + "/" + submonthdir + "/" + subdaydir + "/" + filename.replace(fn,dst.replace('/','')),'w') as writer:
                    writer.write(content)

実行

次の内容通りに実行されます。

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"}
{"name":"水島","age":"52","gender":"1","reg_date":"2020/01/01 17:48:45"}
pi@raspberrypi:~/work/month=01/day=01 $ cd ..
pi@raspberrypi:~/work/month=01 $ cd ..
pi@raspberrypi:~/work $ ls
'month=01'  'month=02'  'month=03'  'month=04'  'month=05'  'month=06'  'month=07'  'month=08'  'month=09'  'month=10'  'month=11'  'month=12'   test.py
pi@raspberrypi:~/work $ python test.py
2020/01/01
2025/01/01
pi@raspberrypi:~/work $ cd month\=01/day\=01
pi@raspberrypi:~/work/month=01/day=01 $ cat cur20250101.json 
{"name":"田中","age":"46","gender":"1","reg_date":"2025/01/01 15:33:25"}
{"name":"鈴木","age":"32","gender":"1","reg_date":"2025/01/01 16:18:12"}
{"name":"水島","age":"52","gender":"1","reg_date":"2025/01/01 17:48:45"}
pi@raspberrypi:~/work/month=01/day=01 $ 

終わりに

ディレクトリとファイルの日時が正しく指定されているのが前提になります。
今回はawsのLinux環境でテストデータファイルの日付変更が必要だったので、
python言語がデフォルトで入っていました。
今日はここまでです。ありがとうございます。

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