大家好我是python脚本的新手,我需要在我的脚本中提供一些帮助
我有像这样的巨大日志文件
xxxx = "qqqqqqq 5466 78455" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 8795 32133" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 5995 21384" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 6546 12346" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 7895 13246" yyyy = "wwww" zzzz = "hhhh" mmmm = "aaaa"
xxxx = "qqqqqqq 64654 94343" yyyy = "wwww" mmmm = "aaaa"
并且此文件继续.....
我需要输出为:
xxxx yyyy zzzz mmmm
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww aaaa
我的代码是
import csv
import re
fread = open(fileread, "r")
resultFile = open("out4.csv",'w')
wr = csv.writer(resultFile)
try:
for line in fread:
regex = re.findall(r'"(.+?)"',line)
wr.writerow(regex)
finally:
fread.close()
resultFile.close()
对于这段代码,我得到的输出为:
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww hhhh aaaa
qqqqqqq wwww aaa
我不知道如何获取标题并根据标题放置数据....
请帮助我或让我了解如何做到这一点
提前致谢!!!!
答案 0 :(得分:0)
如果您的给定文本文件中只有yyyy
和zzzz
值是可选的,则可以使用复杂模式匹配整行:
r'(?:xxxx = "([^"]+)") (?:yyyy = "([^"]+)" )(?:zzzz = "([^"]+)" )?(mmmm = "([^"]+)")'