Skip to content

Instantly share code, notes, and snippets.

@dengshuan
Created May 15, 2014 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dengshuan/d1d983bdddfb0c20392d to your computer and use it in GitHub Desktop.
Save dengshuan/d1d983bdddfb0c20392d to your computer and use it in GitHub Desktop.
Windows/Linux文本文件中文编码乱码问题解决方案
#!/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"
@dengshuan
Copy link
Author

Use python transform.py -2utf8 mygbkfile.txt myutf8file.txt to convert gbk encoded file to utf-8 encoded file
Use python transform.py -2gbk myutf8file.txt mygbkfile.txt to convert utf-8 encoded file to gbk encoded file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment