Created
August 26, 2016 18:56
-
-
Save madox2/5457631660f83ea1262c5e24ddc3acb9 to your computer and use it in GitHub Desktop.
Could be useful when encoding subtitles
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/bin/python | |
import sys | |
import urllib | |
source_enc = 'windows-1250' | |
target_enc = 'utf-8' | |
print '=============================================' | |
print '=== conversion from windows-1250 to utf-8 ===' | |
print '=============================================' | |
print '-> processing arguments' | |
argv = iter(sys.argv) | |
next(argv) | |
for arg in argv: | |
try: | |
filename = str(arg) | |
print '-> processing file ' + filename | |
# prepare user input | |
filename = urllib.unquote(filename).decode('utf-8').strip() | |
if filename.startswith('file://'): | |
filename = filename[7:] | |
print '-> opening file for reading' | |
f = open(filename) | |
content = f.read() | |
f.close() | |
print '-> opening file for writing' | |
f = open(filename, 'w') | |
print '-> encoding file' | |
f.write(unicode(content, source_enc).encode(target_enc)) | |
f.close() | |
print '=============================================' | |
print '================= SUCCESS ===================' | |
print '=============================================' | |
except Exception as e: | |
print '=============================================' | |
print '================== FAIL =====================' | |
print e.__doc__ | |
print '=============================================' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment