Skip to content

Instantly share code, notes, and snippets.

@benjaminestes
Last active November 18, 2022 06:43
Show Gist options
  • Select an option

  • Save benjaminestes/5817f81288ccd89f603862cd9a997735 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminestes/5817f81288ccd89f603862cd9a997735 to your computer and use it in GitHub Desktop.
Convert a UTF-16 file to UTF-8
#!/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