Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]…
- User
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import json import os
def port_json(source_file, target_file): ... with open(source_file, 'r', encoding='utf-8') as f: ... source_data = json.load(f) ... ... with open(target_file, 'r', encoding='utf-8') as f: ... target_data = json.load(f) ... ... for key in source_data: ... if key in target_data: ... target_data[key] = source_data[key] ... ... directory = os.path.dirname(target_file) ... output_file = os.path.join(directory, 'ported.json') ... ... with open(output_file, 'w', encoding='utf-8') as f: ... json.dump(target_data, f, ensure_ascii=False, indent=4) ... print(f"Ported JSON saved to {output_file}") File "", line 1 print(f"Ported JSON saved to {output_file}") IndentationError: unexpected indent
source_file = r'C:\Users\Rushan\Documents\GitHub\Minecraft-Mods-Russian-Translation\1.20\assets\twilightforest\lang\ru_ru.json' target_file = r'C:\Users\Rushan\Documents\GitHub\Minecraft-Mods-Russian-Translation\1.19\to_translate\twilightforest-1.19.2-4.2.1518-universal\assets\twilightforest\lang\en_us.json'
port_json(source_file, target_file)
- Assistant
It looks like there was an indentation error in your code. The
printstatement should not be part of thewithblock. Here's the corrected version of theport_jsonfunction:import json import os def port_json(source_file, target_file): with open(source_file, 'r', encoding='utf-8') as f: source_data = json.load(f) with open(target_file, 'r', encoding='utf-8') as f: target_data = json.load(f) for key in source_data: if key in target_data: target_data[key] = source_data[key] directory = os.path.dirname(target_file) output_file = os.path.join(directory, 'ported.json') with open(output_file, 'w', encoding='utf-8') as f: json.dump(target_data, f, ensure_ascii=False, indent=4) print(f"Ported JSON saved to {output_file}") # Example usage source_file = r'C:\Users\Rushan\Documents\GitHub\Minecraft-Mods-Russian-Translation\1.20\assets\twilightforest\lang\ru_ru.json' target_file = r'C:\Users\Rushan\Documents\GitHub\Minecraft-Mods-Russian-Translation\1.19\to_translate\twilightforest-1.19.2-4.2.1518-universal\assets\twilightforest\lang\en_us.json' port_json(source_file, target_file)