Skip to content

Instantly share code, notes, and snippets.

@ra312
Created August 7, 2020 05:22
Show Gist options
  • Save ra312/fb6792d9b365c1538d014a6fcd45f965 to your computer and use it in GitHub Desktop.
Save ra312/fb6792d9b365c1538d014a6fcd45f965 to your computer and use it in GitHub Desktop.
fix text file encoding
import chardet
import codecs
infile = 'unreadable.txt'
outfile = 'readable.txt'
with open(infile,'rb') as raw_file:
result = chardet.detect(raw_file)
char_enc = result['encoding']
with open(outfile, 'r', charenc):
read_as_utf = f.read()
out = codecs.open(outfile, 'w', 'utf-8')
out.write(read_as_utf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment