Re: How to ignore the first line of the text read from a file
On Aug 28, 3:47 am, Santiago Romero <[EMAIL PROTECTED]> wrote: > > I want to read text line-by-line from a text file, but want to ignore > > only the first line. I know how to do it in Java (Java has been my > > primary language for the last couple of years) and following is what I > > have in Python, but I don't like it and want to learn the better way > > of doing it. > > Why don't you read and discard the first line before processing the > rest of the file? > > file = open(filename, 'r') > file.readline() > for line in file: print line, > > (It works). # You could also do the following: from itertools import islice f = open(filename) for each_line in islice(f, 1, None): print line -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
Santiago Romero wrote: I want to read text line-by-line from a text file, but want to ignore only the first line. I know how to do it in Java (Java has been my primary language for the last couple of years) and following is what I have in Python, but I don't like it and want to learn the better way of doing it. Why don't you read and discard the first line before processing the rest of the file? file = open(filename, 'r') file.readline() for line in file: print line, I believe that file.readline() will work better than file.next() for most purposes since the latter will raise StopIteration on an empty file whereas file.readline() merely returns ''. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
[EMAIL PROTECTED] writes: > If the OP needs line numbers elsewhere in the > code something like the following would work. > > infile = open(fileName, 'r') > for lineNumber, line in enumerate (infile): > # enumerate returns numbers starting with 0. > if lineNumber == 0: continue > print line, This also seems like a good time to mention (untested): from itertools import islice for line in islice(infile, 1, None): print line, -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
On Aug 27, 11:12 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Wed, 27 Aug 2008 21:11:26 -0700, [EMAIL PROTECTED] wrote: > > I want to read text line-by-line from a text file, but want to ignore > > only the first line. I know how to do it in Java (Java has been my > > primary language for the last couple of years) and following is what I > > have in Python, but I don't like it and want to learn the better way of > > doing it. > > > file = open(fileName, 'r') > > lineNumber = 0 > > for line in file: > > if lineNumber == 0: > > lineNumber = lineNumber + 1 > > else: > > lineNumber = lineNumber + 1 > > print line > > > Can anyone show me the better of doing this kind of task? > > input_file = open(filename) > lines = iter(input_file) > lines.next()# Skip line. > for line in lines: > print line > input_file.close() > > Ciao, > Marc 'BlackJack' Rintsch A file object is its own iterator so you can do more simply: input_file = open(filename) input_file.next()# Skip line. for line in input_file: print line, input_file.close() Since the line read includes the terminating EOL character(s), print it with a "print ... ," to avoid adding an additional EOL. If the OP needs line numbers elsewhere in the code something like the following would work. infile = open(fileName, 'r') for lineNumber, line in enumerate (infile): # enumerate returns numbers starting with 0. if lineNumber == 0: continue print line, -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
On Aug 28, 11:53 am, Ken Starks <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Hello, > > > I am new to Python and have one simple question to which I cannot find > > a satisfactory solution. > > I want to read text line-by-line from a text file, but want to ignore > > only the first line. I know how to do it in Java (Java has been my > > primary language for the last couple of years) and following is what I > > have in Python, but I don't like it and want to learn the better way > > of doing it. > > > file = open(fileName, 'r') > > lineNumber = 0 > > for line in file: > > if lineNumber == 0: > > lineNumber = lineNumber + 1 > > else: > > lineNumber = lineNumber + 1 > > print line > > > Can anyone show me the better of doing this kind of task? > > > Thanks in advance. > > LineList=open(filename,'r').readlines()[1,] > for line in Linelist: > blah blah That's bad practice as you load the entire file in memory first as well as it will result in a type error (should be '.readlines()[1:]') -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
> I want to read text line-by-line from a text file, but want to ignore > only the first line. I know how to do it in Java (Java has been my > primary language for the last couple of years) and following is what I > have in Python, but I don't like it and want to learn the better way > of doing it. Why don't you read and discard the first line before processing the rest of the file? file = open(filename, 'r') file.readline() for line in file: print line, (It works). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to ignore the first line of the text read from a file
On Aug 28, 6:11 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hello, > > I am new to Python and have one simple question to which I cannot find > a satisfactory solution. > I want to read text line-by-line from a text file, but want to ignore > only the first line. I know how to do it in Java (Java has been my > primary language for the last couple of years) and following is what I > have in Python, but I don't like it and want to learn the better way > of doing it. > > file = open(fileName, 'r') > lineNumber = 0 > for line in file: > if lineNumber == 0: > lineNumber = lineNumber + 1 > else: > lineNumber = lineNumber + 1 > print line > > Can anyone show me the better of doing this kind of task? > > Thanks in advance. fileInput = open(filename, 'r') for lnNum, line in enumerate(fileInput): if not lnNum: continue print line -- http://mail.python.org/mailman/listinfo/python-list