python实现不同编码的字符的转换
import sys
def filter( srcFile, encode, trgFile ):
error = True
while error:
try:
fs = open( srcFile, encoding = encode )
content = fs.read()
fs.close()
error = False
print( 'Success!' )
except UnicodeDecodeError as ude:
pos1, pos2 = ude.args[2], ude.args[3]
fs = open( srcFile, 'rb' )
content = fs.read()
fs.close()
content = content[:pos1] + content[pos2+1:]
ofs = open( srcFile, 'wb' )
ofs.write( content )
ofs.close()
except:
print( 'Other error happened!' )
break
if __name__ == '__main__':
if len(sys.argv) < 4:
print( 'Usage:' )
print( sys.argv[0] + ' input-file input-encoding output-file' )
else:
filter( sys.argv[1], sys.argv[2], sys.argv[3] )