以下是我的代码:
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更新
KB2980245NT-AUTORITÄT\ SYSTEM 8/18/2014
http://support.microsoft.com/?kbid=2981580 RECHTS更新
KB2981580NT-AUTORITÄT\ SYSTEM 8/18/2014
http://support.microsoft.com/?kbid=2982378 RECHTS安全更新
KB2982378NT-AUTORITÄT\ SYSTEM 9/12/2014
http://support.microsoft.com/?kbid=2984972 RECHTS安全更新
KB2984972NT-AUTORITÄT\ SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2984976 RECHTS安全更新
KB2984976NT-AUTORITÄT\ SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2984981 RECHTS安全更新
KB2984981NT-AUTORITÄT\ SYSTEM 10/16/2014
http://support.microsoft.com/?kbid=2985461 RECHTS更新
KB2985461NT-AUTORITÄT\ SYSTEM 9/12/2014
http://support.microsoft.com/?kbid=2987107 RECHTS安全更新
KB2987107NT-AUTORITÄT\ SYSTEM 10/17/2014
http://support.microsoft.com/?kbid=2990214 RECHTS更新
KB2990214NT-AUTORITÄT\ SYSTEM 4/16/2015
http://support.microsoft.com/?kbid=2991963 RECHTS安全更新
KB2991963NT-AUTORITÄT\ SYSTEM 11/14/2014
http://support.microsoft.com/?kbid=2992611 RECHTS安全更新
KB2992611NT-AUTORITÄT\ SYSTEM 11/14/2014
http://support.microsoft.com/?kbid=2993651 RECHTS更新
KB2993651NT-AUTORITÄT\ SYSTEM 8/29/2014
http://support.microsoft.com/?kbid=2993958 RECHTS安全更新
KB2993958NT-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
中定义的顺序而不是在“ WinUpdates.txt”中出现的顺序打印找到的kbid。如果文件很大(例如,超过一兆字节左右),则对于每个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可以合并为一行。