Python - 从日志文件中提取字符串并将其写入另一个文件

时间:2016-10-02 10:35:21

标签: python file

我有一个如下所示的日志文件:

sw2 switch_has sw2_p3.
sw1 transmits sw2_p2
/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(#t_air_sens2)]),DataHasValue(DataProperty(#qos_type),^^(latency,http://www.xcx.org/1900/02/22-rdf-syntax-ns#PlainLiteral))) */
/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(#t_air_sens2)]),DataHasValue(DataProperty(#topic_type),^^(periodic,http://www.xcx.org/1901/11/22-rdf-syntax-ns#PlainLiteral))) */
...

我感兴趣的是从/* BUG...行中提取特定单词并将它们写入单独的文件中,如下所示:

t_air_sens2 qos_type latency
t_air_sens2 topic_type periodic
...

我可以在awk和shell中的正则表达式的帮助下执行此操作,如下所示:

awk -F'#|\\^\\^\\(' '{for (i=2; i<NF; i++) printf "%s%s", gensub(/[^[:alnum:]_].*/,"",1,$i), (i<(NF-1) ? OFS : ORS) }' output.txt > ./LogErrors/Properties.txt

如何使用Python提取它们? (我应该再使用正则表达式,还是......?)

2 个答案:

答案 0 :(得分:1)

你当然可以使用正则表达式。我会逐行阅读,抓住'/* BUG:'开头的行,然后根据需要解析它们。

import re

target = r'/* BUG:'
bugs = []
with open('logfile.txt', 'r') as infile, open('output.txt', 'w') as outfile:
    # loop through logfile
    for line in infile:
        if line.startswith(target):
            # add line to bug list and strip newlines
            bugs.append(line.strip())
            # or just do regex parsing here
            # create match pattern groups with parentheses, escape literal parentheses with '\'
            match = re.search(r'NamedIndividual\(([\w#]+)\)]\),DataHasValue\(DataProperty\(([\w#]+)\),\^\^\(([\w#]+),', line)
            # if matches are found
            if match:
                # loop through match groups, write to output
                for group in match.groups():
                    outfile.write('{} '.format(group))
                outfile.write('\n')

Python内置了一个非常强大的正则表达式模块:re module

你可以search for a given pattern, then print out the matched groups as needed

注意:raw stringsr'xxxx')允许您使用未转义的字符。

答案 1 :(得分:0)

我尝试了以下方法并获取日志文件的特定行。

target =["BUGS"] # array with specific words

with open('demo.log', 'r') as infile, open('output.txt', 'w') as outfile:

    for line in infile:

        for phrase in target:

            if phrase in line:

                outfile.write('{} '.format(line)) 

这将输出包含目标中单词的行,并将输出写入 output.txt 文件中。