gpt4 book ai didi

python - 在 Python 中查找大型文本文件中的字符串

转载 作者:行者123 更新时间:2023-11-30 23:00:03 25 4
gpt4 key购买 nike

以下是我的代码:

with open("WinUpdates.txt") as f:
data=[]
for elem in f:
data.append(elem)

with open("checked.txt", "w") as f:
check=True
for item in data:
if "KB2982791" in item:
f.write("KB2982791\n")
check=False
if "KB2970228" in item:
f.write("KB2970228\n")
check=False
if "KB2918614" in item:
f.write("KB2918614\n")
check=False
if "KB2993651" in item:
f.write("KB2993651\n")
check=False
if "KB2975719" in item:
f.write("KB2975719\n")
check=False
if "KB2975331" in item:
f.write("KB2975331\n")
check=False
if "KB2506212" in item:
f.write("KB2506212\n")
check=False
if "KB3004394" in item:
f.write("KB3004394\n")
check=False
if "KB3114409" in item:
f.write("KB3114409\n")
check=False
if "KB3114570" in item:
f.write("KB3114570\n")
check=False

if check:
f.write("No faulty Windows Updates found!")

“WinUpdates.txt”文件包含很多像这样的行:

http://support.microsoft.com/?kbid=2980245 RECHTS Update
KB2980245 NT-AUTORITÄT\SYSTEM 8/18/2014
http://support.microsoft.com/?kbid=2981580 RECHTS Update
KB2981580 NT-AUTORITÄT\SYSTEM 8/18/2014
http://support.microsoft.com/?kbid=2982378 RECHTS Security Update KB2982378 NT-AUTORITÄT\SYSTEM 9/12/2014
http://support.microsoft.com/?kbid=2984972 RECHTS Security Update KB2984972 NT-AUTORITÄT\SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2984976 RECHTS Security Update KB2984976 NT-AUTORITÄT\SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2984981 RECHTS Security Update KB2984981 NT-AUTORITÄT\SYSTEM 10/16/2014
http://support.microsoft.com/?kbid=2985461 RECHTS Update
KB2985461 NT-AUTORITÄT\SYSTEM 9/12/2014
http://support.microsoft.com/?kbid=2987107 RECHTS Security Update KB2987107 NT-AUTORITÄT\SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2990214 RECHTS Update
KB2990214 NT-AUTORITÄT\SYSTEM 4/16/2015
http://support.microsoft.com/?kbid=2991963 RECHTS Security Update KB2991963 NT-AUTORITÄT\SYSTEM 11/14/2014
http://support.microsoft.com/?kbid=2992611 RECHTS Security Update KB2992611 NT-AUTORITÄT\SYSTEM 11/14/2014
http://support.microsoft.com/?kbid=2993651 RECHTS Update
KB2993651 NT-AUTORITÄT\SYSTEM 8/29/2014
http://support.microsoft.com/?kbid=2993958 RECHTS Security Update KB2993958 NT-AUTORITÄT\SYSTEM 11/14/2014

但是当我执行代码时,它说它没有找到任何更新?尽管我知道它应该找到4。我将“数据”列表写入一个新的文本文件中,但看起来一切正常?

为什么你认为我的代码不起作用?

最佳答案

FWIW,您的代码可以以更紧凑的方式编写,不需要无数的 if 语句。此外,由于(新)数据文件只有 63342 字节,您可以将整个文件读入单个字符串,而不是读入字符串列表。

kb_ids = (
"KB2982791",
"KB2970228",
"KB2918614",
"KB2993651",
"KB2975719",
"KB2975331",
"KB2506212",
"KB3004394",
"KB3114409",
"KB3114570",
)

with open("WinUpdates.txt") as f:
data = f.read()

check = True
with open("checked.txt", "w") as f:
for kb in kb_ids:
if kb in data:
f.write(kb + "\n")
check = False

if check:
fout.write("No faulty Windows Updates found!\n")

使用链接数据的checked.txt内容:

KB2970228
KB2918614
KB2993651
KB2506212
KB3004394

请注意,此代码按照 kb_ids 中定义的顺序打印找到的 kbid,而不是按照它们在“WinUpdates.txt”中出现的顺序。

如果文件很大,例如超过一兆字节左右,则将整个文件作为字符串搜索每个 kbid 可能不是一个好主意;您可能需要运行一些计时测试(使用 timeit )来查看哪种策略最适合您的数据。

如果您想将文件读入列表中,则无需使用 for 循环,您可以这样做:

with open("WinUpdates.txt") as f:
data = f.readlines()

或者,您可以逐行处理文件,而不将其读入列表:

kb_ids = (
"KB2982791",
"KB2970228",
"KB2918614",
"KB2993651",
"KB2975719",
"KB2975331",
"KB2506212",
"KB3004394",
"KB3114409",
"KB3114570",
)

check = True
with open("WinUpdates.txt") as fin:
with open("checked.txt", "w") as fout:
for data in fin:
for kb in kb_ids:
if kb in data:
fout.write(kb + "\n")
check = False

if check:
fout.write("No faulty Windows Updates found!\n")

在更现代的 Python 版本上,两个 with statements可以组合成一行。

关于python - 在 Python 中查找大型文本文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com