Back to index

plone3  3.1.7
convert_to_utf-8.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys
00004 
00005 def convert(data, encoding='ISO-8859-1'):
00006     data = unicode(data, encoding)
00007     data = data.encode('UTF-8')
00008     return data
00009 
00010 if __name__ == '__main__':
00011     if len(sys.argv) < 3:
00012         print 'Usage: %s <inputfile> <outputfile> [<encoding>]'
00013     infilename = sys.argv[1]
00014     outfilename = sys.argv[2]
00015     encoding = 'ISO-8859-1'
00016     if len(sys.argv) > 3:
00017         encoding = sys.argv[3]
00018     fpi = open(infilename)
00019     try:
00020         data = fpi.read()
00021     finally:
00022         fpi.close()
00023     utfdata = convert(data, encoding)
00024     fpo = open(outfilename, 'wb')
00025     try:
00026         fpo.write(utfdata)
00027     finally:
00028         fpo.close()