0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jsonファイルを編集して上書き保存する

Posted at
sample.py
import json
import copy


def read_json(file_path):
    with open(file_path, "r") as file:
        data = json.load(file)
    return data


def write_json(file_path, data):
    with open(file_path, "w") as file:
        json.dump(data, file, indent=4)


def edit_json(food_dict):
    """nameの値を小文字に変換"""
    edited_data = copy.deepcopy(food_dict)

    for data in edited_data.values():
        data["name"] = data["name"].lower()

    return edited_data


def main():
    file_path = "sample.json"
    food_dict = read_json(file_path)
    edited_data = edit_json(food_dict)
    write_json(file_path, edited_data)


if __name__ == "__main__":
    main()

sample.json
{
    "food1": {
       "name": "APPLE",
       "price": 150
    },
    "food2": {
       "name": "BANANA",
       "price": 100
    }
 }
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?