Files
geojson_conv/main_clear_json_kwarg.py
2024-11-15 15:03:05 +03:00

41 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import os
def clear_string_values(data):
"""
Рекурсивно очищает все строковые значения, оставляя только пустые кавычки (""),
не изменяя значения None или другие типы данных.
"""
if isinstance(data, str):
return ""
elif isinstance(data, list):
return [clear_string_values(item) for item in data]
elif isinstance(data, dict):
return {key: clear_string_values(value) for key, value in data.items()}
return data
def process_json_file(file_path):
"""
Обрабатывает указанный JSON-файл, очищая строковые значения, и сохраняет результат
в новый файл с суффиксом '_cleared'.
"""
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# Очищаем все строковые значения
cleared_data = clear_string_values(data)
# Сохраняем результат в новый файл с суффиксом '_cleared'
new_file_path = f"{os.path.splitext(file_path)[0]}_cleared.json"
with open(new_file_path, 'w', encoding='utf-8') as new_file:
json.dump(cleared_data, new_file, ensure_ascii=False, indent=4)
print(f"Файл с очищенными данными сохранен как: {new_file_path}")
# Укажите путь к вашему JSON-файлу
file_path = 'E:\code\kvant.app\FS_primer.json'
process_json_file(file_path)