Skip to content

Instantly share code, notes, and snippets.

@mateuszgachowski-snippets
Created December 7, 2013 13:09
Show Gist options
  • Save mateuszgachowski-snippets/7841003 to your computer and use it in GitHub Desktop.
Save mateuszgachowski-snippets/7841003 to your computer and use it in GitHub Desktop.
Python: Script to replace subtitles encoding from windows to UTF (QNapi)
#!/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