Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: json2bin.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a JSON file (.json) to a binary file (.bin).
- It reads the JSON file and writes the specified 'text_content' to the binary file using UTF-8 encoding.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'json2bin.py'.
- 2. Ensure your JSON file ('example.json') is in the same directory as the script.
- 3. Run the script.
- 4. The converted binary file ('json2bin.bin') will be generated in the same directory.
- Note: Adjust the 'json_filename' and 'bin_filename' variables in the script as needed.
- """
- import json
- def json_to_bin(json_filename, bin_filename):
- with open(json_filename, 'r') as jsonfile, open(bin_filename, 'wb') as binfile:
- data = json.load(jsonfile)
- if "text_content" in data:
- binfile.write(data["text_content"].encode('utf-8'))
- if __name__ == "__main__":
- json_filename = 'example.json'
- bin_filename = 'json2bin.bin'
- json_to_bin(json_filename, bin_filename)
- print(f"Converted '{json_filename}' to '{bin_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement