▼前提
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
1{ 2 "name": "test", 3 "description": "sample", 4 "URL": "", 5} 6
▼実行しているコード
python
1import json 2import os 3 4dir = "json" 5i = 0 6 7for path in os.listdir(dir): 8 if os.path.isfile(os.path.join(dir, path)): 9 i + 1 10 11 12 13with open('json/test' + str(i+1) + '.json', 'r') as f: 14 json_file = json.load(f) 15 json_file['URL'] = "https://xxxxxxxxxxxxxxxxxxxxxxxxx/" + str(i+1) 16 17 18with open('output/sample' + str(i+1) + ".json", 'w') as f: 19 json.dump(json_file, f, indent=4)
▼実行結果_ファイル名[sample1.json]
json
1{ 2 "name": "test", 3 "description": "sample", 4 "URL": "https://xxxxxxxxxxxxxxxxxxxxxxxxx/1", 5} 6
▼調べたこと
複数のjsonファイルをマージする方法や、
上記のコードも調べて組み合わせて実行してみたものになります。
初心者ですので、このコードが良いのかも分からずに書いております。
▼使っているツール
VScode
python 3.11.0a7+
回答1件
あなたの回答
tips
プレビュー