Python嵌套循环不循环 [英] Python nested loop doesn't loop

查看:245
本文介绍了Python嵌套循环不循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个python初学者,我不能让我的循环。



这是case:我有两个csv文件(事件日志)。



首先是bd8result.csv,有4或5行结构,如下所示:

  2015/10/30 09:53:44,blabla1,259373865,95,F,A1 IP Thers,A1SIP V1(R), -  ,, 1446195224 
2015/10/30 11:03 :14,blabla2,259431070,32,F,A7 IP MornOs,A7SIP V1(R), - 1446199394
2015/10/30 21:30:59,blabla3,259980991,86,F,A2 IP Hor4ain,A2IP V1(R), - ,, 1446237059

第一列是日期,第二列是



我需要找到blabla1,blabla2和blabla3以及事件之前关联的事件这些事件后20分钟,在具有相同结构的更大的日志文件中,并将结果写入csv文件。
我只是收集位于同一个 lecteur 信息的事件(因此在如果... 的测试)。



我的代码看起来像这样:

  with open('result _'+ namefile +'.csv' rb')as master1,open('epoch _'+ namefile +'.csv','rb')as host1:
reader2 = csv.reader(master1)
reader1 = csv.reader(hosts1)
for reader2:
target = row [1]
lecteur = row [5]
epoch_ref = int(row [-1])$ ​​b $ b for row2 in reader1:
epoch1 = int(row2 [-1])$ ​​b $ b lecteur1 = row2 [5]
with open('result_scout'+ namefile +'.csv','a')as results1 :
if epoch1> (epoch_ref_ecart)和epoch1 (epoch_ref + ecart)and lecteur1 == lecteur:
writer1 = csv.writer(results1)
writer1.writerow([target] + [sys.argv [1]] + row2)
我的问题是它正确地执行第一个项目( blabla1 / em>),但不会为blabla2和blabla3写任何内容。



我尝试了几个,但我被困住了。



任何帮助赞赏。谢谢!

解决方案

在循环 reader2 code> reader1 被其for循环用尽,并引发 StopIteration ,并且不会在连续循环中返回任何内容。



您应该在每次迭代中获得 csv.reader 的新实例:

  with open('result _'+ namefile +'.csv','rb')as master1:
for row in csv.reader(master1) :
target = row [1]
lecteur = row
epoch_ref = int(row [-1])$ ​​b $ b with open('epoch _'+ namefile +'。 csv','rb')as host1:
用于csv.reader(hosts1)中的row2:
epoch1 = int(row2 [-1])$ ​​b $ b lecteur1 = row2 [5]
with open('result_scout'+ namefile +'.csv','a')as results1:
if epoch1> (epoch_ref_ecart)和epoch1 (epoch_ref + ecart)and lecteur1 == lecteur:
writer1 = csv.writer(results1)
writer1.writerow([target] + [sys.argv [1]] + row2)
results1.close()

文档


csv.reader(csvfile)



返回一个reader对象,它将在给定的
csvfile中迭代。 csvfile可以是任何支持迭代器
协议的对象,并且每次调用next()方法时都返回一个字符串。


这意味着在读者提出 StopIteration 之后,它将像一个耗尽的发电机一样耗尽。


I'm a python beginner and I can't make my loop loop.

Here's the case : I've got two csv files (event logs).

First on is called bd8result.csv with 4 or 5 lines structured like these :

2015/10/30 09:53:44,blabla1,259373865,95,F,A1 IP Thers,A1SIP V1 (R),-,,1446195224
2015/10/30 11:03:14,blabla2,259431070,32,F,A7 IP MornOs,A7SIP V1 (R),-,,1446199394
2015/10/30 21:30:59,blabla3,259980991,86,F,A2 IP Hor4ain,A2IP V1 (R),-,,1446237059

First column is the date, second is the IP event (target), last on is the epoch time.

I need to find blabla1, blabla2 and blabla3 and events associated 20mn before and 20 minute after these events, in a bigger log file that has the same structure and write the result in a csv file. I just collect events located on the same lecteur info (so comes the test in if...).

My code looks like this :

with open('result_'+ namefile + '.csv', 'rb') as master1, open('epoch_'+ namefile + '.csv', 'rb') as hosts1:
    reader2 = csv.reader(master1)
    reader1 = csv.reader(hosts1)
    for row in reader2:
        target = row[1]
        lecteur = row[5]
        epoch_ref = int(row[-1])
        for row2 in reader1:
            epoch1 = int(row2[-1])
            lecteur1 = row2[5]
            with open('result_scout' + namefile + '.csv', 'a') as results1:
                 if epoch1 > (epoch_ref - ecart) and epoch1 < (epoch_ref + ecart) and lecteur1 == lecteur:
                    writer1 = csv.writer(results1)
                    writer1.writerow([target]+[sys.argv[1]]+row2)
                    results1.close()

My problem is that it executes correctly the first item (blabla1), but doesn' write anything for blabla2 and blabla3.

I've tried several thing but I'm stucked.

Any help appreciated. Thank you!

解决方案

After one iteration of looping over reader2, reader1 is exhausted by its for loop and raises StopIteration and won't return anything in the successive loops.

You should get a new instance of csv.reader in every iteration:

with open('result_'+ namefile + '.csv', 'rb') as master1:
    for row in csv.reader(master1):
        target = row[1]
        lecteur = row[5]
        epoch_ref = int(row[-1])
        with open('epoch_'+ namefile + '.csv', 'rb') as hosts1:
            for row2 in csv.reader(hosts1):
                epoch1 = int(row2[-1])
                lecteur1 = row2[5]
                with open('result_scout' + namefile + '.csv', 'a') as results1:
                     if epoch1 > (epoch_ref - ecart) and epoch1 < (epoch_ref + ecart) and lecteur1 == lecteur:
                        writer1 = csv.writer(results1)
                        writer1.writerow([target]+[sys.argv[1]]+row2)
                        results1.close()

From the documentation:

csv.reader(csvfile)

Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called.

This implies that after the reader raises StopIteration it will be exhausted just like an exhausted generator.

这篇关于Python嵌套循环不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆