On Thu, Oct 16, 2008 at 4:25 AM, Visvaldas K. <[EMAIL PROTECTED]>wrote:
> Hi, > > I feel very stupid, but I am stuck. I could try some workaround, but I want > to understand what I am doing wrong. > > The line: > > for line in open("parameterfile").readlines( ): > print line.split() # trouble line > > > The "trouble line" works fine in this example. It prints > what I want, something which looks like lists of words: > > ['CT', 'CT', '268.0', '1.529']... etc. > > However, if I want to manipulate the individual list elements, I run into > trouble. While > > print line.split()[0] > > still works, (it yields CT - first element), > > however, > > print line.split()[1] > > gives error: > > File "readatoms.py", line 103, in <module> > print line.split()[1] > IndexError: list index out of range > > Could you please tell me what's wrong. (I come from Perl background so > Python seems out-of-the-body experience to me). > > Sincerely, > > Vis > Here is a solution: >>> for line in open('DBAN1001.txt').readlines(): ... line = line.split() ... if len(line) > 1: # This is what makes it work for me. ... print line[1] ... it's have the way that is is prison we It works fine. It seems that in the file you have there are lines whose length is less than you should expect, or maybe a more experienced Python programmer can give you a better answer. > > > > > > > > > > > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com --------------------------------------------------------
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor