Python批量修改文件夹内所有json文件中部分内容

#coding=utf-8 

import os
import json

#获取目标文件夹的路径
filedir = os.getcwd()+'/ceshi'
#获取文件夹中的文件名称列表  
filenames=os.listdir(filedir)
#遍历文件名
for filename in filenames:
    filepath = filedir+'/'+filename
    print filepath

    after = []
    # 打开文件取出数据并修改,然后存入变量
    with open(filepath, 'rb') as f:
        data = json.load(f)
        print type(data)
        for zidian in data:
            if zidian["conference_date"] == "":
                zidian["conference_date"] = "0001-01-01T00:00:00Z"
            if zidian["publication_date"] == "":
                zidian["publication_date"] = "0001-01-01T00:00:00Z"
        after = data

    # 打开文件并覆盖写入修改后内容
    with open(filepath, 'wb') as f:
        data = json.dump(after, f)

 

posted @ 2017-12-07 10:03  右介  阅读(11920)  评论(2编辑  收藏  举报