Created
May 15, 2014 10:21
-
-
Save dengshuan/d1d983bdddfb0c20392d to your computer and use it in GitHub Desktop.
Windows/Linux文本文件中文编码乱码问题解决方案
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/env python | |
import sys, codecs | |
def transform(enc1, enc2, f1, f2): | |
infile = codecs.open(f1, 'rU', enc1) | |
outfile = codecs.open(f2, 'w', enc2, errors='ignore') | |
if enc1 == 'utf-8' and enc2 == 'gbk': | |
for line in infile.readlines(): | |
outfile.write(line[:-1] + '\r\n') | |
elif enc1 == 'gbk' and enc2 == 'utf-8': | |
for line in infile.readlines(): | |
outfile.write(line[:-2] + '\n') | |
infile.close() | |
outfile.close() | |
print 'Transform {} file {} to {} file {} done.'.format(enc1,f1,enc2,f2) | |
if __name__ == '__main__': | |
if len(sys.argv) == 4: | |
if sys.argv[1] == '-2gbk': | |
transform('utf-8', 'gbk', sys.argv[2], sys.argv[3]) | |
elif sys.argv[1] == '-2utf8': | |
transform('gbk', 'utf-8', sys.argv[2], sys.argv[3]) | |
else: | |
print "Invalid command" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
python transform.py -2utf8 mygbkfile.txt myutf8file.txt
to convert gbk encoded file to utf-8 encoded fileUse
python transform.py -2gbk myutf8file.txt mygbkfile.txt
to convert utf-8 encoded file to gbk encoded file