Changing encoding of a text file

Last edited on

The following Python script can be used to change the encoding of a text file.

If invoked without parameters it will print some help and a list of all known encodings (and their aliases).

import codecs
import encodings.aliases
import os.path
import sys

if __name__ == "__main__":
    if len(sys.argv) != 5:
        print
        print "USAGE:"
        print
        print "  " + os.path.basename(sys.argv[0]),
        print "<source> <source_encoding>",
        print "<target> <target_encoding>"
        print
        print "EXAMPLES:"
        print
        print "  " + os.path.basename(sys.argv[0]),
        print "test.log cp850 test.log_utf8 utf8"
        print
        print "SUPPORTED ENCODINGS:"
        l = []
        for k, v in encodings.aliases.aliases.items():
            if k not in l:
                l.append(k)
            if v not in l:
                l.append(v)
        l.sort()
        i = 0
        for enc in l:
            if i % 4 == 0:
                print
                print " ",
            print enc.ljust(18),
            i += 1
            
        sys.exit(1)

    fin = codecs.open(sys.argv[1], "r", sys.argv[2])
    fout = codecs.open(sys.argv[3], "w", sys.argv[4])
    while True:
        s = fin.read(8192)
        if len(s) == 0:
            break
        fout.write(s)
    fin.close()
    fout.close()
recode_txt.py

Examples of invoking this from a Squish test scripts:

import os

def main()
    s = 'python recode_txt.py'
    s += ' "C:\\Users\\Me\\test.txt"'
    s += ' cp1252'
    s += ' "C:\\Users\\Me\\test2.utf8.txt"'
    s += ' utf8'
    os.system(s)
Python
function main() {
    s = 'python recode_txt.py';
    s = s + ' "C:\\Users\\Me\\test.txt"';
    s = s + ' cp1252';
    s = s + ' "C:\\Users\\Me\\test2.utf8.txt"';
    s = s + ' utf8';
    OS.system(s);
}
JavaScript