关于Python文档读取UTF-8编码文件问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq13650793239/article/details/81282591

引用codecs模块,来判断前三个字节是否为BOM_UTF8。如果是,则剔除\xef\xbb\xbf字节

import codecs
def download_ipurl(self,destpath):
    try:
        f = open(destpath,'r')
        iter_f = iter(f)
        lines = 0
        for ipurl in iter_f:
            lines = lines + 1
            if ipurl[0:3] == codecs.BOM_UTF8:    
                self.newipurls.add((ipurl.rstrip('\r\n')).lstrip('\xef\xbb\xbf'))
        #print self.newipurls
        #log记录读取了多少行IP url
        #print lines
    finally:
        if f:
            f.close()

猜你喜欢

转载自blog.csdn.net/qq13650793239/article/details/81282591