Advertisement
Python253

json2bin

Mar 14th, 2024
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: json2bin.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a JSON file (.json) to a binary file (.bin).
  10. It reads the JSON file and writes the specified 'text_content' to the binary file using UTF-8 encoding.
  11.  
  12. Requirements:
  13. - Python 3.x
  14.  
  15. Usage:
  16. 1. Save this script as 'json2bin.py'.
  17. 2. Ensure your JSON file ('example.json') is in the same directory as the script.
  18. 3. Run the script.
  19. 4. The converted binary file ('json2bin.bin') will be generated in the same directory.
  20.  
  21. Note: Adjust the 'json_filename' and 'bin_filename' variables in the script as needed.
  22. """
  23.  
  24. import json
  25.  
  26. def json_to_bin(json_filename, bin_filename):
  27.     with open(json_filename, 'r') as jsonfile, open(bin_filename, 'wb') as binfile:
  28.         data = json.load(jsonfile)
  29.         if "text_content" in data:
  30.             binfile.write(data["text_content"].encode('utf-8'))
  31.  
  32. if __name__ == "__main__":
  33.     json_filename = 'example.json'
  34.     bin_filename = 'json2bin.bin'
  35.     json_to_bin(json_filename, bin_filename)
  36.     print(f"Converted '{json_filename}' to '{bin_filename}'.")
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement