Skip to main content
 首页 » 编程设计

python之使用 Python 从 txt 文件中提取行

2025年05月04日9cyq1162

我正在从 NCBI 下载 mtDNA 记录,并尝试使用 Python 从中提取行。我试图提取的行以某些关键字开头或包含某些关键字,例如“单倍型”和“国籍”或“地区”。我尝试了以下代码:

import re 
infile = open('sequence.txt', 'r') #open in file 'infileName' to read 
outfile = open('results.txt', 'a') #open out file 'outfileName' to write 
 
for line in infile: 
    if re.findall("(.*)haplogroup(.*)", line): 
        outfile.write(line) 
        outfile.write(infile.readline()) 
 
infile.close() 
outfile.close() 

此处的输出仅包含包含“haplogroup”的第一行,例如不包含 infile 中的以下行:

                 /haplogroup="T2b20" 

我还尝试了以下方法:

keep_phrases = ["ACCESSION", "haplogroup"] 
 
for line in infile: 
    for phrase in keep_phrases: 
        if phrase in line: 
            outfile.write(line) 
            outfile.write(infile.readline()) 

但这并没有给我包含 ACCESSION 和 haplogroup 的所有行。

line.startswith 有效,但我不能将它用于单词位于行中间的行。

任何人都可以给我一段代码示例,以将以下行打印到我的输出中以包含“locality”:

/note="origin_locality:Wales" 

关于如何提取包含特定单词的行的任何其他建议也很受欢迎。

编辑:

                 /haplogroup="L2a1l2a" 
                 /note="ethnicity:Ashkenazic Jewish; 
                 origin_locality:Poland: Warsaw; origin_coordinates:52.21 N 
                 21.05 E" 
                 /note="TAA stop codon is completed by the addition of 3' A 
                 residues to the mRNA" 
                 /note="codons recognized: UCN" 

在这种情况下,使用 Peter 的代码,前三行被写入输出文件,但不是包含 21.05 E" 的行。我如何为 /note="设置异常(exception) 并复制所有行,直到第二组引号,而不复制包含 /note="TAA 或 /note 的 /note 行="密码子

编辑2:

这是我目前正在使用的解决方案。

stuff_to_write = [] 
multiline = False 
with open('sequences.txt') as f: 
    for line in f.readlines(): 
        if any(phrase in line for phrase in keep_phrases) or multiline: 
            do_not_write = False 
            if multiline and line.count('"') >= 1: 
                multiline = False 
            if 'note' in line: 
                if any(phrase in line.split('note')[1] for phrase in remove_phrases): 
                    do_not_write = True 
                elif line.count('"') < 2: 
                    multiline = True 
            if not do_not_write: 
                stuff_to_write.append(line) 

请您参考如下方法:

这将在文件中搜索匹配的短语并将这些行写入新文件,假设 "note" 之后的任何内容与 remove_phrases 中的任何内容都不匹配。

它将逐行读取输入以检查是否有任何内容与 keep_phrases 中的单词匹配,将所有值存储在列表中,然后将它们写入单独行的新文件。除非您需要在找到匹配项时逐行写入新文件,否则这种方式应该快得多,因为所有内容都是同时写入的。

如果您不希望它区分大小写,请将 any(phrase in line 更改为 any(phrase.lower() in line.lower() .

keep_phrases = ["ACCESSION", "haplogroup", "locality"] 
remove_phrases = ['codon', 'TAA'] 
 
stuff_to_write = [] 
with open('C:/a.txt') as f: 
    for line in f.readlines(): 
        if any(phrase in line for phrase in keep_phrases): 
            do_not_write = False 
            if 'note' in line: 
                if any(phrase in line.split('note')[1] for phrase in remove_phrases): 
                    do_not_write = True 
            if not do_not_write: 
                stuff_to_write.append(line) 
 
with open('C:/b.txt','w') as f: 
    f.write('\r\n'.join(stuff_to_write))