我在 python 中从 for 循环中打印出许多行,如下所示:
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
'\n' 的存在是为了让 for 循环不只是在一个大块中打印出来,但这也会在每个循环的末尾添加一个空行。目前使用这种方法打印这些:
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
打印self.contents
给出了这个:
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
如何避免 print() 中间出现空行?
编辑:所有这些方法都在 for 循环中调用 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)
印刷品是print wordnet.contents