Python: Convert File Encoding

By Xah Lee. Date: . Last updated: .

Here's a script to convert a file encoding.

# python 3

path1 = "/Users/xah/web/xahlee_info/python/xxtest"
path2 = "/Users/xah/web/xahlee_info/python/xxtest2"

coding1 = "utf-8"
coding2 = "gb18030"

f= open(path1, 'r', encoding=coding1)
content= f.read()
f.close()
f= open(path2, 'w', encoding=coding2)
f.write(content)
f.close()

print("done")

Here's python 2.

# -*- coding: utf-8 -*-
# python 2

path1 = "/Users/xah/web/xahlee_info/python/xxtest"
path2 = "/Users/xah/web/xahlee_info/python/xxtest2"

coding1 = "utf-8"
coding2 = "gb18030"

f= open(path1, 'rb')
content= unicode(f.read(), coding1 )
f.close()
f= open(path2, 'wb')
f.write(content.encode(coding2))
f.close()

print "done"

(thanks to Andrew Clover for help.)

Python Text Processing

File Encoding

Python

Regex

Text Processing

Web

Misc