Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: txt2json.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a text file (.txt) to a JSON file (.json).
- Assumes the entire content of the text file is a single JSON string.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'txt2json.py'.
- 2. Ensure your text file ('example.txt') is in the same directory as the script.
- 3. Run the script.
- 4. The converted JSON file ('txt2json.json') will be generated in the same directory.
- Note: Adjust the 'txt_filename' and 'json_filename' variables in the script as needed.
- """
- import json
- def txt_to_json(txt_filename, json_filename):
- with open(txt_filename, 'r') as txtfile, open(json_filename, 'w') as jsonfile:
- # Assuming the entire content of the text file is a single JSON string
- data = {"text_content": "".join(txtfile)}
- json.dump(data, jsonfile, indent=2)
- if __name__ == "__main__":
- # Set the filenames for the text and JSON files
- txt_filename = 'example.txt'
- json_filename = 'txt2json.json'
- # Convert the text to a JSON file
- txt_to_json(txt_filename, json_filename)
- print(f"Converted '{txt_filename}' to '{json_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement