利用python3修改json文件的指定的value

需求:讀取本地的json 文件,並修改制定的鍵的值,本文主要改寫{'resType':'rest'} 成{'resType':'rect'}

本函數需要給定讀取文件的路徑以及保存的文件的路徑,直接上代碼。有問題請留言

# import module
import os 
import json

def rest_rect(file_old,file_new):
    
    filename_rest = os.listdir(file_old)  # 獲取需要讀取的文件的名字
    L = []

    for rest in filename_rest:
        if os.apth.splitext(rest)[1]='.json':
            L.append(os.path.join(file_old,rest)) #創建文件路徑

    for f11 in L:
        with open(f11,'rw') as f:
            data = json.load(f)
            data[0]['resType'] = 'rect'
            newpath = os.path.join(file_new,os.path.split(f11)[1])
            with open(newpath,'w') as f2:
                json.dump(data,f2)       # 寫入f2文件到本地
# 函數調用 
rest_rect(file_old,file_new)    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章