Skip to content

Instantly share code, notes, and snippets.

@blueset
Created November 11, 2015 02:40
Show Gist options
  • Save blueset/51c9562c3423853890fc to your computer and use it in GitHub Desktop.
Save blueset/51c9562c3423853890fc to your computer and use it in GitHub Desktop.
Convert all *.lrc files in current directory to UTF-8 encoding.
#!/usr/local/bin/python3
# Convert all *.lrc files in current directory to UTF-8 encoding.
__author__ = 'Eana Hufwe <ilove@1a23.com>'
import glob
from chardet.universaldetector import UniversalDetector
detector = UniversalDetector()
import shutil
for filename in glob.glob('*.lrc'):
print (filename.ljust(60))
detector.reset()
with open(filename, 'rb') as f:
for line in f:
detector.feed(line)
if detector.done: break
detector.close()
print (detector.result)
if detector.result['encoding'] not in ['utf-8', 'UTF-8-SIG']:
d = detector.result['encoding']
if d in ['GB2312', None]:
d = 'gbk'
shutil.copy(filename, newfname)
with open(filename, 'rb') as f:
fcont = f.read().decode(d)
newfname = filename+'.bak'
with open(filename, 'wb') as f:
f.write(fcont.encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment