【问题标题】:Finding strings in a large text file in Python在 Python 中查找大型文本文件中的字符串
【发布时间】:2016-05-30 14:51:19
【问题描述】:

以下是我的代码:

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 更新
KB2980245 NT-AUTORITÄT\SYSTEM 2014 年 8 月 18 日
http://support.microsoft.com/?kbid=2981580 RECHTS 更新
KB2981580 NT-AUTORITÄT\SYSTEM 2014 年 8 月 18 日
http://support.microsoft.com/?kbid=2982378 RECHTS 安全更新 KB2982378 NT-AUTORITÄT\SYSTEM 2014 年 9 月 12 日
http://support.microsoft.com/?kbid=2984972 RECHTS 安全更新 KB2984972 NT-AUTORITÄT\SYSTEM 2014 年 10 月 17 日
http://support.microsoft.com/?kbid=2984976 RECHTS 安全更新 KB2984976 NT-AUTORITÄT\SYSTEM 2014 年 10 月 17 日
http://support.microsoft.com/?kbid=2984981 RECHTS 安全更新 KB2984981 NT-AUTORITÄT\SYSTEM 2014 年 10 月 16 日
http://support.microsoft.com/?kbid=2985461 RECHTS 更新
KB2985461 NT-AUTORITÄT\SYSTEM 2014 年 9 月 12 日
http://support.microsoft.com/?kbid=2987107 RECHTS 安全更新 KB2987107 NT-AUTORITÄT\SYSTEM 2014 年 10 月 17 日
http://support.microsoft.com/?kbid=2990214 RECHTS 更新
KB2990214 NT-AUTORITÄT\SYSTEM 2015 年 4 月 16 日
http://support.microsoft.com/?kbid=2991963 RECHTS 安全更新 KB2991963 NT-AUTORITÄT\SYSTEM 2014 年 11 月 14 日
http://support.microsoft.com/?kbid=2992611 RECHTS 安全更新 KB2992611 NT-AUTORITÄT\SYSTEM 2014 年 11 月 14 日
http://support.microsoft.com/?kbid=2993651 RECHTS 更新
KB2993651 NT-AUTORITÄT\SYSTEM 2014 年 8 月 29 日
http://support.microsoft.com/?kbid=2993958 RECHTS 安全更新 KB2993958 NT-AUTORITÄT\SYSTEM 2014 年 11 月 14 日

但是当我执行我的代码时,它说它没有找到任何这些更新?即使我知道它应该找到 4。 我将“数据”列表写入了一个新的文本文件,但似乎一切正常?

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

【问题讨论】:

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

标签: python string list iteration


【解决方案1】:

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 可以合并为一行。

【讨论】:

    【解决方案2】:

    我添加并修复了您缺少的内容,请检查两个 cmets 以了解我的意思。这对我有用,所以它应该对你有用。祝你有美好的一天!

    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!")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2012-05-02
      相关资源
      最近更新 更多