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

在Python中查找大型文本文件中的字符串,python,string,list,iteration,Python,String,List,Iteration,以下是我的代码: 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")

以下是我的代码:

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”文件包含许多类似以下内容的行:

记录更新
KB2980245 NT-AUTORITÄT\SYSTEM 2014年8月18日
记录更新
KB2981580 NT-AUTORITÄT\SYSTEM 2014年8月18日
RECHTS安全更新 KB2982378 NT-AUTORITÄT\SYSTEM 2014年9月12日
RECHTS安全更新 KB2984972 NT-AUTORITÄT\SYSTEM 2014年10月17日
RECHTS安全更新 KB2984976 NT-AUTORITÄT\SYSTEM 2014年10月17日
RECHTS安全更新 KB2984981 NT-AUTORITÄT\SYSTEM 2014年10月16日
记录更新
KB2985461 NT-AUTORITÄT\SYSTEM 2014年9月12日
RECHTS安全更新 KB2987107 NT-AUTORITÄT\SYSTEM 2014年10月17日
记录更新
KB299014 NT-AUTORITÄT\SYSTEM 4/16/2015
RECHTS安全更新 KB2991963 NT-AUTORITÄT\SYSTEM 11/14/2014
RECHTS安全更新 KB2992611 NT-AUTORITÄT\SYSTEM 11/14/2014
记录更新
KB2993651 NT-AUTORITÄT\SYSTEM 2014年8月29日
RECHTS安全更新 KB2993958 NT-AUTORITÄT\系统2014年11月14日

但是当我执行我的代码时,它说它没有找到任何更新?即使我知道它应该找到4。 我把“数据”列表写进了一个新的文本文件,但在那里,一切都好吗


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

我添加并修复了您缺少的内容检查这两条注释以了解我的意思。这对我有用,所以对你也有用。祝你今天愉快

with open("WinUpdates.txt", "r") as f:  #you forgot to put the "r" option to read the file
    data = f.read()  #no reason to put the data into a list a string will do fine

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

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

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")
使用链接数据检查.txt的内容:

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

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

如果要将文件读入列表,则无需对循环使用
,只需执行以下操作:

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版本中,这两种语言可以组合成一行。

与@Math相同;如果我在
WinUpdates.txt
文件中手动添加额外的“KBxxxxxxx”,我会在
checked.txt
中得到多个结果。您得到的是什么输出?您想要什么输出?输出是“checked.txt”文件中的“未找到错误的Windows更新!”,即使它应该写入四个已检查的更新。我不知道是不是文件,导出的WinUpdates.txt导致了问题。与@PM2Ring相同,当我复制并粘贴您在问题中提供的代码时,我在
checked.txt
中得到了“KB2993651”。有了新数据,我什么也得不到。如果在读取文件时打印文件,则会得到
[解码错误-输出不是utf-8]
。所以,这看起来像是一个unicode问题。我在跑蟒蛇2@PM2Ring:我猜你在使用Python3?@SiHa:不,我在使用python2.6.6,但我在使用Linux&我在一个使用UTF-8编码的shell中使用
cat
命令将数据粘贴到一个文件中。