python开发 -- 判断文件编码格式是否为UTF8 有/无BOM格式

简单检测脚本:

import codecs
file_path = '/Users/mac/Downloads/test.xml'

with open(file_path) as source_file:
    data = source_file.read()
    if data[:3] == codecs.BOM_UTF8:
        print('******* Have BOM  *******')
    else:
        print('******* No BOM  *******')

参考:

https://www.cnblogs.com/Detector/p/8744992.html

https://www.cnblogs.com/Detector/p/8483010.html

 

posted @ 2020-05-08 13:23  hello-Jesson  阅读(1163)  评论(0编辑  收藏  举报