Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: json_to_text.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a JSON file (.json) to a text file (.txt).
- It reads the JSON file, pretty-prints its content, and saves it as a text file.
- The output text file is formatted with indentation for better readability.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'json_to_text.py'.
- 2. Ensure your JSON file ('example.json') is in the same directory as the script.
- 3. Run the script.
- Note: Adjust the 'input_json_file' and 'output_text_file' variables in the script as needed.
- """
- import json
- import os
- def json_to_text(input_json_file, output_text_file):
- input_json_path = os.path.join(os.getcwd(), input_json_file)
- output_text_path = os.path.join(os.getcwd(), output_text_file)
- with open(input_json_path, 'r', encoding='utf-8') as json_file:
- data = json.load(json_file)
- with open(output_text_path, 'w', encoding='utf-8') as text_file:
- text_file.write(json.dumps(data, indent=2, ensure_ascii=False))
- if __name__ == "__main__":
- input_json_file = 'example.json'
- output_text_file = 'json2txt.txt'
- json_to_text(input_json_file, output_text_file)
- print(f"Converted '{input_json_file}' to '{output_text_file}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement