Python从日志文件中提取字符串并将它们写入另一个fi

2024-10-03 15:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个日志文件如下:

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...行中提取特定的单词并将它们写入单独的文件中,如下所示:

^{pr2}$

我可以借助shell中的awk和regex来完成此操作,如下所示:

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提取它们?(我应该再次使用regex,还是….?)在


Tags: 文件httptypeairbugtoocomplexaxiom
1条回答
网友
1楼 · 发布于 2024-10-03 15:21:32

当然可以使用regex。我将逐行阅读,获取以'/* 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内置了一个非常强大的regex模块:re module

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

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

相关问题 更多 >