▼前提
pythonもjsonも扱いがほとんどできないです。
質問の仕方すら正しいか不明ですが、お知恵お借りできますと幸いです。
▼やりたいこと
1000のjsonファイルに対して1つのファイルごとに'値'を更新したい
更新する値は変数で1 ~ 1000と連続する数字を入れたい
▼発生していること・つまずいていること
1つのファイルに対してやりたいことを実行することはできた。
しかし、1つのファイルしか処理ができず、2~1000のjsonに対しても、
同様の処理を行いたい
▼フォルダ構造
json
┣test1.json
┣test2.json
┣test3.json
•
•
•
┗test1000.json
▼jsonの中身_ファイル名[test1.json]
json
{ "name": "test", "description": "sample", "URL": "", }
▼実行しているコード
python
import json import os dir = "json" i = 0 for path in os.listdir(dir): if os.path.isfile(os.path.join(dir, path)): i + 1 with open('json/test' + str(i+1) + '.json', 'r') as f: json_file = json.load(f) json_file['URL'] = "https://xxxxxxxxxxxxxxxxxxxxxxxxx/" + str(i+1) with open('output/sample' + str(i+1) + ".json", 'w') as f: json.dump(json_file, f, indent=4)
▼実行結果_ファイル名[sample1.json]
json
{ "name": "test", "description": "sample", "URL": "https://xxxxxxxxxxxxxxxxxxxxxxxxx/1", }
▼調べたこと
複数のjsonファイルをマージする方法や、
上記のコードも調べて組み合わせて実行してみたものになります。
初心者ですので、このコードが良いのかも分からずに書いております。
▼使っているツール
VScode
python 3.11.0a7+
まだ回答がついていません
会員登録して回答してみよう