正则表达式从文件中提取字符串列表

regex extract list of strings from file

我有一个输入文件 (input.txt),其中包含一些遵循类似于以下行的标准格式的数据:

<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Politische Inklusion"@de .
<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Political inclusion"@en .
<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Radiologische Kampfmittel"@de . 

我想提取位于 outputfile-en.txt 中的“”@en 之间的英语字符串列表,以及位于 outputfile-de.txt 中的“@de”之间的德语字符串列表

在此示例中,输出文件-en.txt 应包含:

Political inclusion 

和输出文件-de.txt 应包含:

Politische Inklusion
Radiologische Kampfmittel 

哪个正则表达式适合这里?

你可以这样做:

import re

str = """<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Politische Inklusion"@de .
<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Political inclusion"@en .
<descriptor/nnn> <http://www.nnn.org/2004/02/skos/core#prefLabel> "Radiologische Kampfmittel"@de . """

german = re.compile('"(.*)"@de')
english = re.compile('"(.*)"@en')

print german.findall(str)
print english.findall(str)

这会给你 ['Politische Inklusion', 'Radiologische Kampfmittel'] 和 ['Political inclusion']。 现在您只需迭代这些结果并将它们写入适当的文件。

有了这样一个简单的模式,根本不需要正则表达式,尤其是不需要重复相同的数据来选择不同的语言——您可以流式分析并即时写入结果:

with open("input.txt", "r") as f:  # open the input file
    file_handles = {}  # a map of our individual output file handles
    for line in f:  # read it line by line
        rindex = line.rfind("@")  # find the last `@` character
        language = line[rindex+1:rindex+3]  # grab the following two characters as language
        if rindex != -1:  # char found, consider the line...
            lindex = line.rfind("\"", 0, rindex-1)  # find the preceding quotation
            if lindex != -1:  # found, we have a match
                if language not in file_handles:  # add a file handle for this language:
                    file_handles[language] = open("outputfile-{}.txt".format(language), "w")
                # write the found slice between `lindex` and `rindex` + a new line
                file_handles[language].write(line[lindex+1:rindex-1] + "\n")
    for handle in file_handles.values():  # lets close our output file handles
        handle.close()

应该比 regex + 快得多,因为它适用于任何语言,所以如果你有 ...@it 行,它也会节省 outputfile-it.txt