Python正则表达式匹配多个并写入文件

Python正则表达式匹配多个并写入文件,python,regex,csv,Python,Regex,Csv,我有一个文件,其中存储了来自多个Windows服务器的事件,这些事件是不分开的(所有导出都在一个长行中,有时是选项卡式的,有时不是) 我已经构建了regex来提取我想要存储在CSV中的信息(总共20个匹配组),但是我现在尝试循环文件中的许多行,并以CSV格式输出相关信息。我的代码如下: #!/usr/bin/env python import csv import re match1 = re.compile('(\D{3} \d+ \d{2}[:]\d{2}[:]\d{2})') <

我有一个文件,其中存储了来自多个Windows服务器的事件,这些事件是不分开的(所有导出都在一个长行中,有时是选项卡式的,有时不是)

我已经构建了regex来提取我想要存储在CSV中的信息(总共20个匹配组),但是我现在尝试循环文件中的许多行,并以CSV格式输出相关信息。我的代码如下:

#!/usr/bin/env python
import csv
import re

match1 = re.compile('(\D{3}  \d+ \d{2}[:]\d{2}[:]\d{2})')
<snipped for brevity>
match20 = re.compile('(?:Logon Type:  (.+?)   )')

regexes = [match1, match2, match3, match4, match5, match6, match7, match8, match9, match10, match11, match12, match13, match14, match15, match16, match17, match18, match19, match20]

pattern_combined = '|'.join(x.pattern for x in regexes)
print(pattern_combined)
print("End of combined regexes\n\n\n")

with open('Argus.csv', 'r') as fobj:
        with open('Argus1.csv', 'w') as resultFile:
                reader = csv.reader(fobj, delimiter=',')
                for row in reader:
                        output = re.findall(r'pattern_combined',row)
                        print(output)
                        print("End of OUTPUT\n\n\n")
                        out_str = ','.join(output)
                        print(out_str)
                        print("Final output\n\n\n")
                        resultFile.write(out_str)

正如您所看到的,相当标准的Windows事件格式,只是通过一些我无法控制的系统日志进程被推送到一个文件中,很遗憾

如果您发布一个您试图解析的示例文本文件和预期结果,我们可能会更好地为您提供帮助。当然,只需要清理一些主机名等,我会将其编辑为问题,thanksr'pattern_combined'将文字字符串'pattern_combined'转换为原始字符串。考虑更改<代码>输出=Re.FiDALL(R'PATNNY组合,行)< /C> >将Topnnl组合变量的值转换为RWSTRIGTEST:<代码>输出= Re.FUNDALL(Topnn-组合,行)< /代码>
<13>Mar  8 05:56:00 PHCHBS-TP320161.XX.domain.net MSWinEventLog     4       Security        364388  Thu Mar 08 05:56:00 2018        4634    Microsoft-Windows-Security-Auditing     DOMAINNET\SYS_APM_XXXXXX1     N/A     Success Audit   PHCHBS-TP320161.XX.DOMAIN.net Logoff          An account was logged off.    Subject:   Security ID:  S-1-5-21-2025429265-1383384898-682003330-43805   Account Name:  SYS_APM_XXXXXX1   Account Domain:  DOMAINNET   Logon ID:  0xb1dfe060    Logon Type:   3    <truncated 199 bytes>       364387