vendredi 1 avril 2016

Need understanding of this python error

I am actually processing a text log file, with the following python code. this runs continuosly even after the mline reaches EOF.

myfile = open("560A_HL_Japan_02_04_2016.txt", 'r')
mod_myfile = open("560A_HL_Japan_02_04_2016_modified.txt", "wb")
mfl = myfile.readlines()
mstring=''
for mline in mfl:
    mli = mline.split()
    for l in range(len(mli)):
        if l >= 2:                      #second object
            mstring += mli[l]+' '
    mstring += '\n'
    mod_myfile.write(mstring)
mod_myfile.close()

If I make a slight modification with the below code. it executes without any issues

myfile = open("560A_HL_Japan_02_04_2016.txt", 'r')
mod_myfile = open("560A_HL_Japan_02_04_2016_modified.txt", "wb")
mfl = myfile.readlines()
for mline in mfl:
    mli = mline.split()
    for l in range(len(mli)):
        if l == 2:                      #second object
            mstring = mli[l]+' '
        elif l > 2:
            mstring += mli[l]+' '
    mstring += '\n'
    mod_myfile.write(mstring)
 mod_myfile.close()

Aucun commentaire:

Enregistrer un commentaire