【问题标题】:Python - Calling lines from a text file to compile a pattern search of a second filePython - 从文本文件中调用行以编译第二个文件的模式搜索
【发布时间】:2014-11-06 10:43:39
【问题描述】:

如果有人问及回答,请原谅我。如果是这样,请将其归结为我是编程新手,并且对正确搜索的了解不够。

我需要读入包含一系列数百个短语的文件,例如姓名或电子邮件地址,每行一个,用作已编译搜索词的一部分 - pattern = re.search(name) . 'pattern' 变量将用于搜索另一个超过 500 万行的文件,以识别并从相关行中提取选择字段。

为变量读入的名称文件的文本格式为:

John\n
Bill\n
Harry@helpme.com\n
Sally\n

到目前为止,我的以下代码不会出错,但也不会处理和关闭。如果我使用带有 sys.argv[1] 的稍微不同的代码手动传递名称,一切正常。粗体代码(应该是)是我遇到问题的区域 - 从“lines = open....”开始

import sys
import re
import csv
import os

searchdata = open("reallybigfile", "r")
Certfile = csv.writer(open('Certfile.csv', 'ab'), delimiter=',')

**lines = open("Filewithnames.txt", 'r')
while True:
    for line in lines:
        line.rstrip('\n')
        lines.seek(0)
        for nam in lines:
             pat = re.compile(nam)**

for f in searchdata.readlines():
    if pat.search(f):
        fields = f.strip().split(',') 
        Certfile.writerow([nam, fields[3], fields[4]])
lines.close()

底部的代码(从“for f in searchdata.readlines():”开始)很好地定位、提取和写入字段。我一直无法找到读取 Filewithnames.txt 文件并让它使用每一行的方法。它要么挂起,就像这段代码一样,要么将文件的所有行读取到最后一行并仅返回最后一行的数据,例如'莎莉'。

提前致谢。

【问题讨论】:

  • 你能从每个输入文件中添加几行样本吗?
  • 输入文件与上面的类似,但通常是firstname.lastname。 realbig 文件的示例行可能看起来像 2014-06-03,2017-06-02,12345678901234567,5336789,40803A,"ACTIVE","bb-klm-ssl-Unc-1y-v6","AE" ,"cn=JOHN L. MELLON 123420,uid=john.mellon@helpme.com,ou=people,o=internal,dc=helpme,dc=com" 但行可能会有所不同。一致性在于我们正在寻找的搜索词。它并不总是符合上面显示的配置文件

标签: python python-2.7 python-3.x


【解决方案1】:

while True 是一个无限循环,我可以看到没有办法摆脱它。这肯定会导致程序永远继续运行而不抛出错误。

删除while True 行并取消缩进该循环的代码,看看会发生什么。

编辑:

正如评论的那样,我已经解决了一些问题,但我会让您找出实现目标所需的精确正则表达式。

import sys
import re
import csv
import os

searchdata = open("c:\\dev\\in\\1.txt", "r")
# Certfile = csv.writer(open('c:\\dev\\Certfile.csv', 'ab'), delimiter=',') #moved to later to ensure the file will be closed

lines = open("c:\\dev\\in\\2.txt", 'r')
pats = []   # An array of patterns
for line in lines:
    line.rstrip()
    lines.seek(0)
    # Add additional conditioning/escaping of input here.
    for nam in lines:
         pats.append(re.compile(nam))

with open('c:\\dev\\Certfile.csv', 'ab') as outfile:    #This line opens the file
    Certfile = csv.writer(outfile, delimiter=',')       #This line interprets the output into CSV
    for f in searchdata.readlines():
        for pat in pats:    #A loop for processing all of the patterns
            if pat.search(f) is not None:
                fields = f.strip().split(',') 
                Certfile.writerow([pat.pattern, fields[3], fields[4]])
lines.close()
searchdata.close()

首先,确保关闭所有文件,包括您的输出文件。 如前所述,while True 循环导致您无限运行。 您需要一个正则表达式或一组正则表达式来涵盖所有可能的“名称”。执行一组正则表达式的代码更简单,所以这就是我在这里所做的。这可能不是最有效的。这包括处理所有模式的循环。

我相信您需要对输入文件进行额外的解析才能为您提供干净的正则表达式。我给你留了一些空间让你去做。

希望有帮助!

【讨论】:

  • 删除 'while True' 停止了挂起,但它恢复为仅向文件的最后一行提供响应数据。
  • 带有 pat= 的行应该进行追加而不是简单的分配。可能还有一两个错误。
  • 我的无知在这里显示。我没有遵循附加指南。需要 re.compile 从名称/电子邮件地址创建正则表达式。它用于 realbigfile,以定位每行多个字段之一中的引用以及长字母数字特殊字符串的一部分。我将如何或附加什么?谢谢。
  • 因此您需要一个正则表达式,其中包含“Filewithnames.txt”中的一组名称。您需要将该正则表达式(即那组名称)与“reallybigfile”中的每一行进行比较。目前,您正在从“filewithnames.txt”中的集合生成许多单独的正则表达式并覆盖它。
  • 今天早上我在附加上顿悟了,然后当我阅读你的代码时意识到我仍然错了。附加在读入时工作正常,是的,我需要清理我的输入。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2013-12-06
  • 2011-07-10
  • 2020-01-25
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
相关资源
最近更新 更多