Reputation: 1425
I have numerous lines getting printed out in python from for loops like below:
class Wordnet():
def compose_line6(self, pointers, pointers_synset_type):
for A, B in zip(pointers, pointers_synset_type):
self.line6 += 'http://www.example.org/lexicon#'+A+' http://www.monnetproject.eu/lemon#pos '+B+'\n'
return self.line6
def compose_line7(self, pointers, pointer_source_target):
for A, B in zip(pointers, pointer_source_target):
self.line7 += 'http://www.example.org/lexicon#'+A+' http://www.monnetproject.eu/lemon#source_target '+B+'\n'
return self.line7
The '\n' is there so that the for loops don't just get printed out in a big block, but this also add a blank line to the end of each loop. printing these at the moment using this method:
def compose_contents(self, line1, line2, line3, line4, line5, line6, line7):
self.contents = '\n'.join([line1, line2, line3, line4, line5, line6, line7])
return self.contents
and printing self.contents
gives this:
http://www.example.org/lexicon#13796604 http://www.monnetproject.eu/lemon#pos n
http://www.example.org/lexicon#00603894 http://www.monnetproject.eu/lemon#pos a
http://www.example.org/lexicon#00753137 http://www.monnetproject.eu/lemon#pos v
http://www.example.org/lexicon#01527311 http://www.monnetproject.eu/lemon#pos v
http://www.example.org/lexicon#02361703 http://www.monnetproject.eu/lemon#pos v
http://www.example.org/lexicon#13796604 http://www.monnetproject.eu/lemon#source_target 0000
http://www.example.org/lexicon#00603894 http://www.monnetproject.eu/lemon#source_target 0401
http://www.example.org/lexicon#00753137 http://www.monnetproject.eu/lemon#source_target 0302
http://www.example.org/lexicon#01527311 http://www.monnetproject.eu/lemon#source_target 0203
http://www.example.org/lexicon#02361703 http://www.monnetproject.eu/lemon#source_target 0101
How do avoid the blank line in the middle of the print()?
EDIT: all of these methods are called in a for loop def line_for_loop(self, file): for line in file: self.compose_line6(self.pointers, self.pointers_synset_type) self.compose_line7(self.pointers, self.pointer_source_target) self.compose_contents(self.line1, self.line2, self.line3, self.line4, self.line5, self.line6, self.line7)
and the print is print wordnet.contents
Upvotes: 0
Views: 1562
Reputation: 1124438
Filter out empty lines:
def compose_contents(self, *lines):
self.contents = '\n'.join([line for line in lines if line.strip()])
return self.contents
This supports an arbitrary number of input lines by using the *args
arbitrary positional argument syntax.
Upvotes: 2