Skip to content

Instantly share code, notes, and snippets.

@jerrylususu
Created March 30, 2018 17:16
Show Gist options
  • Save jerrylususu/bb664173ffba2d63bd9af62188980630 to your computer and use it in GitHub Desktop.
Save jerrylususu/bb664173ffba2d63bd9af62188980630 to your computer and use it in GitHub Desktop.
UTF-8 Fixer
# utf8-fixer
import os
import chardet
s = os.sep
dirname = "C:\Projects\Assignment1"
# your assignment dir goes here
for root, dirs, files in os.walk(dirname):
for file in files:
if(file=="comments.txt"):
print(os.path.join(root, file))
f = open(os.path.join(root, file),'rb')
data = f.read()
f.close()
guess_encoding = chardet.detect(data)['encoding']
print(guess_encoding)
isutf8 = (guess_encoding == "utf-8")
print(isutf8)
if(not isutf8):
f = open(os.path.join(root, file),'r',encoding=guess_encoding)
content = f.read();
print(content)
f.close()
f = open(os.path.join(root, file),'w',encoding="utf-8")
f.write(content)
f.close()
print("written")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment