Good morning Kumar, I believe you need to do something like:
# # Read from command line supplied input file # for line in open(sys.argv[1]).xreadlines(): # # Remove \n from end of line # line=line[:-1] # # Break line by tab delimiter and feed to list # split_line_by_tab=line.split('\t') You should then be able to print out list items relatively easily. Thank you, Andrew Robert Systems Architect Information Technology - OpenVMS Massachusetts Financial Services Phone: 617-954-5882 Pager: 781-764-7321 E-mail: [EMAIL PROTECTED] Linux User Number: #201204 -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of kumar s Sent: Thursday, December 09, 2004 11:51 AM To: [EMAIL PROTECTED] Subject: [Tutor] Difference between for i in range(len(object)) and for i in object Dear group, My Tab delimited text looks like this: HG-U95Av2 32972_at 432 117 HG-U95Av2 32972_at 499 631 HG-U95Av2 32972_at 12 185 HG-U95Av2 32972_at 326 83 HG-U95Av2 32972_at 62 197 I want to capture: columns 2 and 3 as tab delim. text: Here is my code: >>> spot_cor=[] >>> for m in cor: ... cols = split(cor,'\t') ... spot_cor.append(cols[2]+'\t'+cols[3]) ... ... Traceback (most recent call last): File "<stdin>", line 2, in ? File "/usr/local/lib/python2.3/string.py", line 121, in split return s.split(sep, maxsplit) AttributeError: 'list' object has no attribute 'split' Here is 2nd way: >>> test_cor=[] >>> for m in cor: ... cols = split(cor,'\t') ... x = (cols[2]+'\t'+cols[3]) ... test_cor.append(x) ... Traceback (most recent call last): File "<stdin>", line 2, in ? File "/usr/local/lib/python2.3/string.py", line 121, in split return s.split(sep, maxsplit) AttributeError: 'list' object has no attribute 'split' Here is my 3rd way of doing this thing: >>> for m in range(len(cor)): ... cols = split(cor[m],'\t') ... spot_cor.append(cols[2]+'\t'+cols[3]) ... >>> >>> len(spot_cor) 2252 >>> My question: Many people suggested me to avoid iteration over a object using (range(len)) its index and use instead 'Python's power' by using for i in object, instead. However, when I tried that using some data, as demonstrated above, I get error because append method does not work on list. In method 2, i tried to append an object instead of string elements. In both ways the execution failed because 'List object has no attribute split'. Can you help me making me clear about his dogma. Thank you. Kumar. --- Guillermo Fernandez Castellanos <[EMAIL PROTECTED]> wrote: > Cheers, > > I think your mistake is here: > if x == y: > for ele3 in spot_int: > if y in ele3: > > out.write(ele3) > > out.write('\n') > Each time you find an element that is the same > (x==y) you don't write > only y, you write *all* the elements that are in > spot_init instead > only the matching one! And it's not what you are > looking for! :-) > > I'll also change a bit your code to make it look > more "pythonic" :-) > > > for ele1 in spot_cor: > > for ele2 in spot_int: > > cols = split(ele2,'\t') > > y = (cols[0]+'\t'+cols[1]) > > if ele1 == y: > > for ele3 in spot_int: > > if y in ele3: > > > out.write(ele3) > > > out.write('\n') > > What changes I did: > > for ele1 in range(len(spot_cor)): > x = spot_cor[ele1] > > can be writen like: > for ele1 in spot_cor: > x = ele1 > > Furthermore, as you only use x once, I changed: > if x == y: > > with > if ele1 == y: > > and deleted the line: > x = ele1 > > I also don't understand why you do this: > cols = split(ele2,'\t') > y = (cols[0]+'\t'+cols[1]) > > It seems to me that you are separating something to > put it again > together. I don't really see why... > > Enjoy, > > Guille > __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 _______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor "MFS Relay Service" made the following annotations on 12/09/2004 12:05:50 PM ------------------------------------------------------------------------------ This email communication and any attachments may contain proprietary, confidential, or privileged information. If you are not the intended recipient, you are hereby notified that you have received this email in error and that any review, disclosure, dissemination, distribution or copying of it or its contents is prohibited. The sender does not waive confidentiality or any privilege by mistransmission. If you have received this email in error, please notify the sender immediately, delete this email, and destroy all copies and any attachments. ============================================================================== _______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor