python 去除微软的BOM

傻逼微软会给文件前面加上efbbbf, 导致开发人员浪费很多时间在排错上,下面通过python代码来实现去除微软BOM的功能

用法很简单,指定可能含有BOM开头的文件,并且将微软的\r\n 换成linux的\n ,作用不影响原文件,会生产一个副本

 

import sys

if len(sys.argv) < 2 :
    print "please special the filename that maybe have BOM(SB Microsoft Files)";
    sys.exit(2);

filename = sys.argv[1];

content = open(filename, "r").read().encode("hex");
if "efbbbf" in content:
    print "Oh God!!, the file have stepped on the Microsoft shit";
    print "Cleaning it now!!"
    print "======================================================="
    print content
    content = content.replace("efbbbf","");
    content = content.replace("0d0a","0a");

open(filename+"_noBOM","w").write(content.decode('hex'));

 

 

 

 

posted @ 2018-06-09 16:16  Khazix  阅读(370)  评论(0编辑  收藏  举报