TCAX 字幕特效制作工具官方论坛 | ASS | TCAS | Python | Aegisub | Lua
标题:
Python处理utf-8 添加和删除BOM头
[打印本页]
作者:
Seekladoom
时间:
2021-10-7 10:01:17
标题:
Python处理utf-8 添加和删除BOM头
来源链接:
https://blog.csdn.net/weixin_30621711/article/details/98361204
以下代码只处理了assic和utf8文件。其它文件编码为保险起见并未加入支持。
参数
exts 需要处理文件的扩展名
folders 需要处理的文件夹及子目录
处理目录为当前目录
运行:
添加bom头
python proc_bom.py
删除bom头
python proc_bom.py -r
运行缺少chardet报错 请查看:
http://www.cnblogs.com/wangmh/p/8004451.html
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os;
import sys;
import codecs;
import chardet;
#获取脚本文件的当前路径
def cur_file_dir():
#获取脚本路径
path = sys.path[0]
#判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
#打印结果
#pip install chardet 安装相应插件
def procBOM(strPath,curLen, bAdd):
newcontent = '';
f = open(strPath, "rb");
fcontent = f.read();
f.close();
printBuffer = strPath[curLen:]
codeType = chardet.detect(fcontent)["encoding"] #检测编码方式
printBuffer = printBuffer + " "+str(codeType)
if codeType.lower().find('utf-8') == -1 and codeType.lower().find('ascii') == -1 :
#非utf8文件保险起见先退出,并输出错误提示,todo后续再添加其它转码到utf8
print printBuffer + " error OK"
return
#不需要转换,已经添加bom头
if bAdd and fcontent[:3] != codecs.BOM_UTF8:
print printBuffer+" add bom",
newcontent = codecs.BOM_UTF8;
newcontent += fcontent;
elif not bAdd and fcontent[:3] == codecs.BOM_UTF8:
newcontent = fcontent[3:];
print printBuffer+" del bom",
else:
return;
fnew = open(strPath, "wb+")
fnew.write(newcontent);
fnew.close();
print "done"
return
if __name__ == "__main__":
bAdd = True;
exts = ['.py'];
folders = ["GNaviInterface/search","src","tester"]
bAdd = True;
if(len(sys.argv) > 1 and sys.argv[1] == '-r'):
bAdd = False;
curLen = len(cur_file_dir())
for folderName in folders:
folderPath = cur_file_dir()+"/"+folderName+"/"
#print "procBOM:folder path = "+folderPath+",add = "+str(bAdd)
for parent,dirnames,filenames in os.walk(folderPath):
for f in filenames:
bTargetFile = False;
for e in exts:
if(f.endswith(e)):
bTargetFile = True;
if(bTargetFile):
procBOM(os.path.join(parent,f),curLen, bAdd);
#print 'file:%s add:%s' % (os.path.join(parent, f), bAdd);
复制代码
转载于:
https://www.cnblogs.com/wangmh/p/8005388.html
欢迎光临 TCAX 字幕特效制作工具官方论坛 | ASS | TCAS | Python | Aegisub | Lua (http://tcax.org/)
Powered by Discuz! X2