Python re.findall输出到CSV只有有时

问题描述:

有一些代码来找到一个字符串中的几个匹配的术语,在我的情况下,一个日志文件,我试图输出实例到一个csv的工作,但只有时。如果我有太多的变量,它似乎打破并输出一个空白的CSV,否则它实际上工作。Python re.findall输出到CSV只有有时

作品:

z = re.findall("(?<=ID\=)\w+", resp) 
rec = re.findall("(?<=RECEIVED\=)\w+", resp) 

with open('/out.csv','w') as file: 
    for x,y in zip(z,rec): 
     file.write(x + ',' +y) 
     file.write('\n') 

给了我一个空白CSV:

i = re.findall("(?<=ID\=)\w+", resp) 
rec = re.findall("(?<=RECEIVED\=)\w+", resp) 
da = re.findall("(?<=DA\=)\w+", resp) 
oa = re.findall("(?<=OA\=)\w+", resp) 
st = re.findall("(?<=DELIVERED\=)\w+", resp) 
pr = re.findall("(?<=PRICE\=)\w+", resp) 
net = re.findall("(?<=NETWORK\=)\w+", resp) 
cn = re.findall("(?<=COUNTRY\=)\w+", resp) 
gw = re.findall("(?<=GATEWAY\=)\w+", resp) 
msg = re.findall("(?<=MSG\=)\w+", resp) 

file = (i + ',' + rec + ',' + da + ',' + oa + ',' + st + ',' + pr + ',' + net + ',' + cn + ',' + gw + ',' + msg) 
with open('out.csv','w') as file: 
    for a,b,c,d,e,f,g,h,j,k in zip(i,rec,da,oa,st,pr,net,cn,gw,msg): 
     file.write(a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f + ',' + g + ',' + h + ',' + j + ',' + k) 
     file.write('\n') 

也许我是在想这或做错误的方式。基本上希望输出成为csv格式的这些输出。如果我正在愚蠢地随意嘘我。

使用包含熊猫的函数pd.to_csv(...)并将信息存储在DataFrame中会不会容易得多?

将DataFrame写入逗号分隔值(csv)文件。见 Pandas

喜欢的东西:

i = re.findall("(?<=ID\=)\w+", resp) 
rec = re.findall("(?<=RECEIVED\=)\w+", resp) 
da = re.findall("(?<=DA\=)\w+", resp) 
oa = re.findall("(?<=OA\=)\w+", resp) 
st = re.findall("(?<=DELIVERED\=)\w+", resp) 
pr = re.findall("(?<=PRICE\=)\w+", resp) 
net = re.findall("(?<=NETWORK\=)\w+", resp) 
cn = re.findall("(?<=COUNTRY\=)\w+", resp) 
gw = re.findall("(?<=GATEWAY\=)\w+", resp) 
msg = re.findall("(?<=MSG\=)\w+", resp) 

indices = ("i", "rec", "da", "oa", "st", "pr", "net", "cn", "gw", "msg") 

data = pd.DataFrame(data=zip(i, rec, da, oa, st, pr, net, cn, gw, msg), index=indices) 
pd.DataFrame.to_csv(data, "out.csv") 

@JSimonsen: 让我们来看看这是否会取得成功?

i = ['one', 'two'] 
rec = ['three', 'four'] 
da = ['five', 'six'] 
oa = ['seven', 'eight'] 

indices = ["col1", "col2"] 

df = pd.DataFrame(data=zip(i, rec, da, oa), index=indices) 
df.to_csv('out.csv') 

因为我不知道你在使用数据,这只是一个简单的例子,我可以尝试,但re.findall()返回strings列表。因此,它应该工作...

+0

嗯,这似乎是一个更好的办法,不是我做的,但最好的一切都是为了给我玩了是文字打印:我 REC 达 OA ST pr net cn gw msg在csv vs变量本身的数据 – JSimonsen

+0

是的,这是完全有效的。看起来我的数据非常具体,有时会打破它。我只需要精确定义我的正则表达式,以便它不寻找其他与之相匹配的东西。谢谢! – JSimonsen

+0

不客气! – Albo