python 在JSON文件中搜索值,替换它并保存转换(深度 - 周围)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在JSON文件中搜索值,替换它并保存转换(深度 - 周围)相关的知识,希望对你有一定的参考价值。

"""KEYWORD REPLACING MODULE."""
import os
import json

# functions
def get_files():
    """lists files"""
    exclude = set(['.vscode', 'sample'])
    json_files = []
    for root, dirs, files in os.walk(os.getcwd(), topdown=True):
        dirs[:] = [d for d in dirs if d not in exclude]
        for name in files:
            if name.endswith('.json'):
                json_files.append(os.path.join(root, name))
    return json_files

def load_files(json_files):
    """works files"""
    for json_file in json_files:
        with open(json_file) as js:
            loaded_json = json.load(js)
            # get_recursively(loaded_json, os.path.basename(json_file))
            print get_recursively(loaded_json, 'name')


def write_file(data_file, new_file_name):
    """writes the file"""
    if not os.path.exists('converted'):
        os.makedirs('converted')
    with open('converted/' + new_file_name, 'w') as json_file:
        json.dump(data_file, json_file)

def get_recursively(search_dict, field):
    """
    Takes a dict with nested lists and dicts,
    and searches all dicts for a key of the field
    provided.
    """
    fields_found = []

    for key, value in search_dict.iteritems():

        if key == field:
            fields_found.append(value)

        elif isinstance(value, dict):
            results = get_recursively(value, field)
            for result in results:
                if SEARCH_KEY in result:
                    fields_found.append(result)

        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict):
                    more_results = get_recursively(item, field)
                    for another_result in more_results:
                        if SEARCH_KEY in another_result:
                            fields_found.append(another_result)

    return fields_found
    # write_file(js_file, js_file_name)

# main
print "\n" + '- on ' + os.getcwd()
NEW_DIR = raw_input('Work dir (leave empty if current): ')
if not NEW_DIR:
    print NEW_DIR
    NEW_DIR = os.getcwd()
else:
    print NEW_DIR
    os.chdir(NEW_DIR)

# get_files()
JS_FILES = get_files()
print '- files on ' + os.getcwd()
# print "\n".join(JS_FILES)
SEARCH_KEY = raw_input('Value to search: ')
# RKEY = raw_input('Replacement value: ')
load_files(JS_FILES)

以上是关于python 在JSON文件中搜索值,替换它并保存转换(深度 - 周围)的主要内容,如果未能解决你的问题,请参考以下文章

在javascript中遍历嵌套的json文件时替换值

json文件中文字改变但是网页却不变

如何用python查找和替换json文件中的特定字符串

有没有pickle的替代品 - 保存字典(python)

如何使用 Python 或 posix 工具搜索和替换字符串中的匹配括号

用 CSV 中的值替换字符串的一部分,将所有值保存在 CSV PYTHON