Created
December 7, 2013 13:09
-
-
Save mateuszgachowski-snippets/7841003 to your computer and use it in GitHub Desktop.
Python: Script to replace subtitles encoding from windows to UTF (QNapi)
This file contains 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 | |
# -*- coding: utf-8 -*- | |
import sys, codecs, os | |
replacements = { | |
u'¹' : u'ą', | |
u'³' : u'ł', | |
u'œ' : u'ś', | |
u'æ' : u'ć', | |
u'¿' : u'ż', | |
u'ê' : u'ę', | |
u'Ÿ' : u'ź', | |
u'ñ' : u'ń', | |
u'Œ' : u'Ś', | |
u'£' : u'Ł' | |
} | |
def replace_all(text, dic): | |
for i, j in dic.iteritems(): | |
text = text.replace(i, j) | |
return text | |
if len(sys.argv) > 1: | |
inputFileName = sys.argv[1] | |
else: | |
sys.exit('Please provide a filename') | |
targetFileName = os.path.basename(inputFileName)[:-4]+'.replaced.txt' | |
try: | |
inputFile = codecs.open(inputFileName, 'r', 'windows-1252') | |
newFile = codecs.open(targetFileName, 'w', 'utf-8') | |
for line in inputFile.readlines(): | |
line = replace_all(line, replacements) | |
newFile.write(line) | |
print "File saved as " + targetFileName | |
except: | |
print "I cannot open the file you have provided" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment