Last active
November 18, 2022 06:43
-
-
Save benjaminestes/5817f81288ccd89f603862cd9a997735 to your computer and use it in GitHub Desktop.
Convert a UTF-16 file to UTF-8
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 python | |
| # Invocation: | |
| # ./fixutf.py input_file.csv | |
| # | |
| # Outputs to | |
| # utf8-[input_file.csv] | |
| import sys | |
| def utf16_to_utf8(path): | |
| try: | |
| with open(path, "rb") as source: | |
| with open("utf8-{0}".format(path), "wb") as dest: | |
| dest.write(source.read().decode("utf-16").encode("utf-8")) | |
| except FileNotFoundError: | |
| print("😰 That file doesn't seem to exist.") | |
| def main(argv): | |
| try: | |
| utf16_to_utf8(argv[1]) | |
| except IndexError: | |
| print("😵 Expected name of file to convert!") | |
| if __name__ == "__main__": | |
| main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment