Skip to content

Instantly share code, notes, and snippets.

@WANGJIEKE
Created September 10, 2019 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WANGJIEKE/a4dc6bea3c009a2fe0e9e99e3c93d02f to your computer and use it in GitHub Desktop.
Save WANGJIEKE/a4dc6bea3c009a2fe0e9e99e3c93d02f to your computer and use it in GitHub Desktop.
GBK to UTF-8 in Python 3
#!/usr/bin/env python3
from typing import Optional
import os
import fire
def gbk2utf8(path: str, out: Optional[str]='utf8_out.txt') -> None:
"""Convert file encoded in GBK into UTF-8
:param path: path to the file encoded in GBK
:param out: optional output UTF-8 file path (default is utf8_out.txt)
"""
with open(os.path.expanduser(path), 'rb') as gbk_file:
with open(os.path.expanduser(out), 'w') as utf8_file:
utf8_file.write(gbk_file.read().decode('gbk'))
if __name__ == '__main__':
fire.Fire(gbk2utf8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment