[Tutor] Removing certain sequences from a string list elements

Varsha Purohit varsha.purohit at gmail.com
Sat Jan 7 02:08:54 CET 2012


Hello,

I have a simple python program where I am comparing two log files and I am
storing the differences in a list. I am programming in python after a long
time so may be I might have not written something very efficient. Please
let me know what alternate solution I can apply for my program.

I am reading each line in the file individually and storing them in a list.
After that i am comparing the two lists and printing out the differences.
But I wanted to know how can I iter through each string in the list and
remove certain sequences like \n and ',' comma. I want to basically
printout a column where it has each element of the list in each row.
It should also avoid priting the time stamp since they will be different
anyway. i want the program as simple as it looks right now.

Input file contains something like this in each line. I have not included
the complete log file.

*MegaMon> mfc*
*MFC data:*
*    vendorId/deviceId=1000/005b, subVendorId/subDeviceId=1000/9285, OEM=1,
SubOem=1, isRaidKeySecondary=0*
*    MFCF: disableSAS=0, maxDisks=0, enableRaid6=1, disableWideCache=0*
*    disableRaid5=0, enableSecurity=0, enableReducedFeatureSet=0*
*    enableCTIO=0 enableSnapshot=1 enableSSC=1 enableCacheOffload=0*
*    maxHANodes=2*


here is the program

def readList1():
    f1 = open('mfc_node1.txt',"r")
    lines = f1.read().split(" ")
    q = []
    for line in lines:
        if not line in q:
            q.append(line)
    f1.close()
    return q

def readList2():
    f = open('mfc_node2.txt',"r")
    lines = f.read().split(" ")
    p = []
    for line in lines:
        if not line in p:
            p.append(line)
    f.close()
    return p



if __name__ == "__main__":
    q = readList1()
    #print q
    p = readList2()
    #print p
    newList = []

    for x in q:
        if x not in p:
            newList.append(x)


Here is the part of the output list
['enableCTIO=0,', 'enableSnapshot=1,', 'enableSSC=1,', 'maxHANodes=0\n',
'sasAddr=5123456712345678\nSecondary', 'enableSnapshot=0,', 'enableSSC=0,',
'sasAddr=0000000000000000\nMegaMon>', '13:53:14:']

-Varsha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120106/4e293a4c/attachment-0001.html>


More information about the Tutor mailing list