首页 > 解决方案 > 将 codecs.open() 与 python 一起使用时运行时间增加

问题描述


我想从日志文件中获取特定信息并通过一些字符串对其进行过滤。我选择使用codecs.open,因为我收到如下错误消息

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 3167: invalid start byte.

问题不在于编码不合适,如 utf-16。

这样做使错误消失了,但现在这个脚本比以前花费了更长的时间。有什么办法可以优化它以减少运行时间?

我的代码看起来很像这样:

listeFull = codecs.open("file", "r",encoding='utf-8', errors='ignore')
strings = ("str1","str2","str3")
net = "0.0.0.0"
for line in listeFull:
        if net in line:
            if all(s not in line for s in strings):
                print(line)
listeFull.close()

标签: pythonruntime

解决方案


推荐阅读