Re: print odd numbers of lines from tekst WITHOUT space between lines
On Saturday, February 18, 2017 at 6:03:37 PM UTC, Wildman wrote: > On Sat, 18 Feb 2017 09:38:32 -0800, TTaglo wrote: > > > i = 1 > > f = open ('rosalind_ini5(1).txt') > > for line in f.readlines(): > > if i % 2 == 0: > > print line > > i += 1 > > > > > > How do i get output without breaks between the lines? > > > > Result: > > > > Other things just make you swear and curse > > > > When you're chewing on life's gristle, don't grumble give a whistle > > > > This will help things turn out for the best > > > > Always look on the bright side of life > > In Python 3 you can do this: > > print(line, end="") > > For Python 2 use this: > > import sys > . > . > . > sys.stdout.write(line) > > Don' forget... > f.close() > > -- > GNU/Linux user #557453 > The cow died so I don't need your bull! For Python 2, strictly from memory:- from __future__ import print_function print(line, end="") Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
TTaglo wrote: > i = 1 > f = open ('rosalind_ini5(1).txt') > for line in f.readlines(): > if i % 2 == 0: > print line > i += 1 > > > How do i get output without breaks between the lines? I'm late to the show, but here are a few unsolicited remarks to your code. (1) The best way to open a file is with open(filename) as f: # use the file # at this point the file is closed without the need to invoke # f.close() explicitly (2) The readlines() method reads the complete file into a list. This can become a problem if the file is large. It is better to iterate over the file directly: for line in f: # use the line (3) If you read the file into a list you can get all odd lines with for odd_line in f.readlines()[1::2]: # use the odd line (4) If you follow my advice from (2) and want to avoid the in-memory list there's an equivalent itertools.islice() function that you can use like this: import itertools for odd_line in itertools.islice(f, 1, None, 2): # use the odd line (5) sys.stdout.write() was already mentioned, but there is also a sys.stdout.writelines() meathod which takes a sequence of strings. With that your code may become import itertools import sys with open('rosalind_ini5(1).txt') as f: sys.stdout.writelines(itertools.islice(f, 1, None, 2)) -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
Op zaterdag 18 februari 2017 18:55:46 UTC+1 schreef boB Stepp: > On Sat, Feb 18, 2017 at 11:38 AM, TTaglowrote: > > i = 1 > > f = open ('rosalind_ini5(1).txt') > > for line in f.readlines(): > > if i % 2 == 0: > > print line > > i += 1 > > > > > > How do i get output without breaks between the lines? > > If you use "print line," (Note the trailing comma.), this should > suppress the line break that the print statement normally inserts. > > Another suggestion might be to use enumerate() instead of using a > manual counter in your for loop. > > HTH! > > > > -- > boB thanks for the help ! -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
Op zaterdag 18 februari 2017 18:38:43 UTC+1 schreef TTaglo: > i = 1 > f = open ('rosalind_ini5(1).txt') > for line in f.readlines(): > if i % 2 == 0: > print line > i += 1 > > > How do i get output without breaks between the lines? > > Result: > > Other things just make you swear and curse > > When you're chewing on life's gristle, don't grumble give a whistle > > This will help things turn out for the best > > Always look on the bright side of life thank you all! -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
On Sat, 18 Feb 2017 09:38:32 -0800, TTaglo wrote: > i = 1 > f = open ('rosalind_ini5(1).txt') > for line in f.readlines(): > if i % 2 == 0: > print line > i += 1 > > > How do i get output without breaks between the lines? > > Result: > > Other things just make you swear and curse > > When you're chewing on life's gristle, don't grumble give a whistle > > This will help things turn out for the best > > Always look on the bright side of life In Python 3 you can do this: print(line, end="") For Python 2 use this: import sys . . . sys.stdout.write(line) Don' forget... f.close() -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
On Sat, 18 Feb 2017 11:55:34 -0600, boB Stepp wrote: > On Sat, Feb 18, 2017 at 11:38 AM, TTaglowrote: >> i = 1 f = open ('rosalind_ini5(1).txt') >> for line in f.readlines(): >> if i % 2 == 0: >> print line >> i += 1 >> >> >> How do i get output without breaks between the lines? > > If you use "print line," (Note the trailing comma.), this should > suppress the line break that the print statement normally inserts. > > Another suggestion might be to use enumerate() instead of using a manual > counter in your for loop. > > HTH! or simply strip the cr/lf of the end of each line -- Smell from unhygenic janitorial staff wrecked the tape heads -- https://mail.python.org/mailman/listinfo/python-list
Re: print odd numbers of lines from tekst WITHOUT space between lines
On Sat, Feb 18, 2017 at 11:38 AM, TTaglowrote: > i = 1 > f = open ('rosalind_ini5(1).txt') > for line in f.readlines(): > if i % 2 == 0: > print line > i += 1 > > > How do i get output without breaks between the lines? If you use "print line," (Note the trailing comma.), this should suppress the line break that the print statement normally inserts. Another suggestion might be to use enumerate() instead of using a manual counter in your for loop. HTH! -- boB -- https://mail.python.org/mailman/listinfo/python-list
print odd numbers of lines from tekst WITHOUT space between lines
i = 1 f = open ('rosalind_ini5(1).txt') for line in f.readlines(): if i % 2 == 0: print line i += 1 How do i get output without breaks between the lines? Result: Other things just make you swear and curse When you're chewing on life's gristle, don't grumble give a whistle This will help things turn out for the best Always look on the bright side of life -- https://mail.python.org/mailman/listinfo/python-list