【问题标题】:Use a file to search lines in another file in Python在 Python 中使用文件搜索另一个文件中的行
【发布时间】:2015-07-15 17:13:53
【问题描述】:

我有两个文件:一个每行一个单词,另一个有 3 个;它们看起来像这样:

列表文件:

Gene1
Gene2
Gene3
Gene4

主文件:

Gene8   Gene3   2.1
Gene10  Gene5   3
Gene1   Gene20  2.1
Gene3   Gene2   3.3 
Gene48  Gene95  2

所以我想要的是使用列表文件来搜索和提取主文件中与列表匹配的行并将它们写入第三个新文件中。所以期望的输出是:

新文件:

Gene8   Gene3   2.1
Gene1   Gene20  2.1
Gene3   Gene2   3.3

我曾尝试使用正则表达式来使用 re.search,但我似乎并没有得到正确的结果,因为它总是在匹配的情况下写入整个文档,而不是单独的匹配行。

我尝试加载文件并将它们转换为字符串并使用双 for 循环,但看起来它是逐字匹配而不是逐字匹配,这使得输出文件很难管理。

是的,我看到了Use Python to search lines of file for list entries 的帖子,但我无法使其正常工作,并且生成的文件还需要更多格式,这使过程变得复杂,而且我似乎丢失了一些信息(列表文件有数千个条目和主文件是几十万行,所以不容易跟踪)。

我来找你,因为我知道应该有一种更高效、更简单的方法,因为它需要运行多次

【问题讨论】:

  • 与您的 RAM 相比,列表文件有多大?
  • grep -f ListFile MasterFile > NewFile 怎么样? (ListFile每行末尾加一个空格,避免匹配Gene10Gene20Gene48
  • 您可以在每行末尾添加一个空格sed 's/$/ /' oldfile > newfile
  • @NightShadeQueen Mmm 6k 长,4GB RAM
  • @timrau 是的,有人告诉我 grep 会很简单,虽然我没有 UNIX 计算机的访问权限,所以我认为如果它在 UNIX 中足够简单,那么应该有一个足够简单的解决方案蟒蛇

标签: python search writing


【解决方案1】:

将关键字列表加载到集合中:

keywords = set()
with open(list_file_path) as list_file:
    for line in list_file:
        if line.strip():
            keywords.add(line.strip())

然后遍历主文件中的每一行,取出至少包含一个关键字的行:

with open(master_file_path) as master_file:
    with open(search_results_path, 'w') as search_results:
        for line in master_file:
            if set(line.split()[:-1]) & keywords:
                search_results.write(line)

【讨论】:

  • 谢谢,这似乎工作得很好,很有效,而且可以理解:)
【解决方案2】:

应该这样做。我使用了您提供的两个示例数据文件,下面的代码提供了您发布的所需输出。如果要经常重复此过程并且您需要加快速度,那么您可能需要考虑使用不同的搜索算法。如果是这种情况,请告诉我哪些操作最常见(插入列表、搜索列表、删除列表中的项目),我们可以使用最合适的搜索算法。

# open the list of words to search for
list_file = open('list.txt')

search_words = []

# loop through the words in the search list
for word in list_file:

    # save each word in an array and strip whitespace
    search_words.append(word.strip())

list_file.close()

# this is where the matching lines will be stored
matches = []

# open the master file
master_file = open('master.txt')

# loop through each line in the master file
for line in master_file:

    # split the current line into array, this allows for us to use the "in" operator to search for exact strings
    current_line = line.split()

    # loop through each search word
    for search_word in search_words:

        # check if the search word is in the current line
        if search_word in current_line:

            # if found then save the line as we found it in the file
            matches.append(line)

            # once found then stop searching the current line
            break

master_file.close()


# create the new file
new_file = open('new_file.txt', 'w+')

# loop through all of the matched lines
for line in matches:

    # write the current matched line to the new file
    new_file.write(line)

new_file.close()

【讨论】:

  • 除了搜索和提取之外没有太多操作。虽然对于这么大的文件来说它有点慢,但它确实有效。这是一些非常简洁的注释代码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多