jeudi 27 octobre 2016

How can I rewrite the following to avoid a "Nesting Blocks" error?

The following code generates

SystemError: too many statically nested blocks

import csv
with open('complexdatset.csv','r'), open ('back.csv','w'), open ('buffer_overflow.csv','w'), open ('ftp_write.csv','w'), open ('guess_passwd.csv','w'), open ('imap.csv','w'), open ('ipsweep.csv','w'), open ('land.csv','w'), open ('loadmodule.csv','w'), open ('multihop.csv','w'), open ('neptune.csv','w'), open ('nmap.csv','w'), open ('perl.csv','w'), open ('phf.csv','w'), open ('pod.csv','w'), open ('portsweep.csv','w'), open ('rootkit.csv','w'), open ('satan.csv','w'), open ('smurf.csv','w'), open ('spy.csv','w'), open ('teardrop.csv','w'), open ('warezclient.csv','w'), open ('warezmaster.csv','w') as fin, back, buffer_overflow, ftp_write, guess_passwd, imap, ipsweep, land, loadmodule, multihop, neptune, nmap, perl, phf, pod, portsweep, rootkit, satan, smurf, spy, teardrop, warezclient, warezmaster:
    writer0 = csv.writer(back, delimiter=',')
    writer1 = csv.writer(buffer_overflow, delimiter=',')
    writer2 = csv.writer(ftp_write, delimiter=',')
    writer3 = csv.writer(guess_passwd, delimiter=',')
    writer4 = csv.writer(imap, delimiter=',')
    writer5 = csv.writer(land, delimiter=',')
    writer6 = csv.writer(loadmodule, delimiter=',')
    writer7 = csv.writer(multihop, delimiter=',')
    writer8 = csv.writer(neptune, delimiter=',')
    writer9 = csv.writer(nmap, delimiter=',')
    writer10 = csv.writer(perl, delimiter=',')
    writer11 = csv.writer(phf, delimiter=',')
    writer12 = csv.writer(pod, delimiter=',')
    writer13 = csv.writer(portsweep, delimiter=',')
    writer14 = csv.writer(rootkit, delimiter=',')
    writer15 = csv.writer(satan, delimiter=',')
    writer16 = csv.writer(smurf, delimiter=',')
    writer17 = csv.writer(spy, delimiter=',')
    writer18 = csv.writer(teardrop, delimiter=',')
    writer19 = csv.writer(warezclient, delimiter=',')
    writer20 = csv.writer(warezmaster, delimiter=',')
    for row in csv.reader(fin, delimiter=','):
        if row[41] == 'back':
            writer0.writerow(row)
        elif row[41] == 'buffer_overflow':
            writer1.writerow(row)
        elif row[41] == 'ftp_write':
            writer2.writerow(row)
        elif row[41] == 'guess_passwd':
            writer3.writerow(row)
        elif row[41] == 'imap':
            writer4.writerow(row)
        elif row[41] == 'land':
            writer5.writerow(row)
        elif row[41] == 'loadmodule':
            writer6.writerow(row)
        elif row[41] == 'multihop':
            writer7.writerow(row)
        elif row[41] == 'neptune':
            writer8.writerow(row)
        elif row[41] == 'nmap':
            writer9.writerow(row)
        elif row[41] == 'perl':
            writer10.writerow(row)
        elif row[41] == 'phf':
            writer11.writerow(row)
        elif row[41] == 'pod':
            writer12.writerow(row)
        elif row[41] == 'portsweep':
            writer13.writerow(row)
        elif row[41] == 'rootkit':
            writer14.writerow(row)
        elif row[41] == 'satan':
            writer15.writerow(row)
        elif row[41] == 'smurf':
            writer16.writerow(row)
        elif row[41] == 'spy':
            writer17.writerow(row)
        elif row[41] == 'teardrop':
            writer18.writerow(row)
        elif row[41] == 'warezclient':
            writer19.writerow(row)   
        elif row[41] == 'warezmaster':
            writer20.writerow(row)

My question is why does it generate this error? I searched online but all the examples had loops nested 20 levels deep. I don't have the file I/O nested that much at all. I just make very extensive checks for one variable and create files 2 levels deep. My goal was to create individual '.csv' files for each of the different classes of this particular dataset. Also is there a better way to rewrite this so I can avoid the SystemError?

Aucun commentaire:

Enregistrer un commentaire