[Tutor] Removing certain sequences from a string list elements
Hugo Arts
hugo.yoshi at gmail.com
Tue Jan 10 19:05:33 CET 2012
On Sat, Jan 7, 2012 at 2:08 AM, Varsha Purohit <varsha.purohit at gmail.com> wrote:
> 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
>
>
These two functions should be one function that takes a filename as
argument. They are exactly the same. For removing commas and newlines,
you should google the str.strip() method.
More information about the Tutor
mailing list