python - 将 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()
解决方案
推荐阅读
- javascript - 我需要在 JavaScript 中获取任意两个日期之间的所有日期
- python - 使用 Python 从 CSV 文件中删除新行
- sql - 当 SQL Server 中的另一列为真时对一列求和
- silverlight-4.0 - Silverlight 4 应用程序在 visual-studio-2015 中的 silverlight 5 中运行
- c++ - 创建了不同类型的节点
- typescript - 打字稿动态类型和泛型
- hyperledger-fabric - 无法在超级账本 Fabric 中启动 Raft 共识中的订购者
- android - 加载初始数据时分页库不会触发观察
- push-notification - 带有 .NET 的 ActiveMQ
- c# - 与 localhost C# 的双向通信