python解决js文件utf-8编码乱码问题(推荐)
html文件中引入js文件,显示乱码!
js文件为utf-8 编码(无bom) ,此时只要将js文件转成utf-8 BOM编码就可以解决了
可以使用notepad++转码
也可以使用下面的python代码批量转码
# -*- coding:utf-8 -*- import os,sys import chardet def convert( filename, in_enc = "GBK", out_enc="UTF-8" ): try: print("convert " + filename) f = open(filename,'rb') content = f.read() result = chardet.detect(content)#通过chardet.detect获取当前文件的编码格式串,返回类型为字典类型 print(result) f.close() coding = result.get('encoding')#获取encoding的值[编码格式] if coding != 'UTF-8-SIG' and coding == 'utf-8':#文件格式如果是utf-8的时候,才进行转码 print(coding + " to "+ out_enc +"!") new_content = content.decode(in_enc).encode(out_enc) f = open(filename, 'wb') f.write(new_content) f.close() print(" done") else: print(coding) except IOError as e: # except: print(e) def explore(dir): for root, dirs, files in os.walk(dir): for file in files: path = os.path.join(root, file) convert(path) def main(dir): if(os.path.isdir(dir)): fpaths = [fpath for fpath in os.listdir(dir) if os.path.isfile(dir+"\\"+fpath) and fpath.endswith('.js')] dpaths = [dpath for dpath in os.listdir(dir) if os.path.isdir(dir+"\\"+dpath)] for f in fpaths: convert(dir+"\\"+f,'utf-8','UTF-8-SIG') for d in dpaths: print(d) main(dir+"\\"+d) if __name__ == "__main__": main('目录')
总结
相关推荐
88274956 2020-11-03
runner 2020-09-01
梦的天空 2020-08-25
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
lyqdanang 2020-08-16
MyNameIsXiaoLai 2020-07-08
星辰的笔记 2020-07-04
csstpeixun 2020-06-28
letheashura 2020-06-26
liaoxuewu 2020-06-26
sunzhihaofuture 2020-06-21
FEvivi 2020-06-16
坚持着执着 2020-06-16
waterv 2020-06-14
xiaoge00 2020-06-14
firejq 2020-06-14
firstboy0 2020-06-14
e度空间 2020-06-12
zhongweinan 2020-06-10