Last active
September 30, 2022 12:16
-
-
Save SeanPesce/72a3bf33bdd6d91da638e4eba504441f to your computer and use it in GitHub Desktop.
Python 3 script to ASCII-encode a JSON file with UTF-8 data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Author: Sean Pesce | |
import json | |
import sys | |
def json_convert_utf8_to_ascii_file(in_fpath, out_fpath, include_encoding=False): | |
b = b'' | |
with open(in_fpath, 'rb') as f: | |
b = f.read() | |
j = json.loads(b) | |
if include_encoding: | |
j['encoding'] = 'ascii' # If you want to include encoding metadata | |
with open(out_fpath, 'w') as f: | |
json.dump(j, f, indent=2, ensure_ascii=True) | |
return | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print(f'Usage:\n\t{sys.argv[0]} <UTF8 JSON input file> <ASCII JSON output file>') | |
sys.exit() | |
json_convert_utf8_to_ascii_file(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment