Advertisement
Python253

json2txt

Mar 5th, 2024
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: json2txt.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. JSON to Text Converter
  8.  
  9. This Python script converts the contents of a JSON file into plain text and saves the result in a text file.
  10. The output text file is formatted with indentation for better readability.
  11.  
  12. Usage:
  13. 1. Ensure you have Python 3 installed.
  14. 2. Replace 'input.json' and 'output.txt' with your actual JSON input file and desired text output file names.
  15. 3. Run the script, and the converted text will be saved in the specified output file.
  16.  
  17. Requirements:
  18. - Python 3
  19.  
  20. Example Usage:
  21. python json2txt.py
  22. """
  23.  
  24. import json
  25. import os
  26.  
  27. def json_to_text(input_json_file, output_text_file):
  28.     input_json_path = os.path.join(os.getcwd(), input_json_file)
  29.     output_text_path = os.path.join(os.getcwd(), output_text_file)
  30.  
  31.     with open(input_json_path, 'r', encoding='utf-8') as json_file:
  32.         data = json.load(json_file)
  33.  
  34.     with open(output_text_path, 'w', encoding='utf-8') as text_file:
  35.         text_file.write(json.dumps(data, indent=2, ensure_ascii=False))
  36.  
  37. # Replace 'input.json' and 'output.txt' with your actual file names
  38. json_to_text('input.json', 'output.txt')
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement