Created
November 11, 2015 02:40
-
-
Save blueset/51c9562c3423853890fc to your computer and use it in GitHub Desktop.
Convert all *.lrc files in current directory to UTF-8 encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
# Convert all *.lrc files in current directory to UTF-8 encoding. | |
__author__ = 'Eana Hufwe <[email protected]>' | |
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