我正试图在几个循环中打印出一行的行,并且我想找到一种打印行而不用的方法\n,因为在每个循环完成后都会添加一个空行。我拥有的代码示例如下:def compose_line6(self, pointers, pointers_synset_type): self.line6 = '' 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.line6def compose_line7(self, pointers, pointer_source_target): self.line7 = '' 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.line7def compose_contents(self, line1, line2, line3, line4, line5, line6, line7): self.contents = '''\ '''+line1+''' '''+line2+''' '''+line3+''' '''+line4+''' '''+line5+''' '''+line6+''' '''+line7+'''''' return self.contentsdef print_lines(self, contents): print (contents)当我打印这些时,会发生以下情况: http://www.example.org/lexicon#13796604 http://www.monnetproject.eu/lemon#pos nhttp://www.example.org/lexicon#00603894 http://www.monnetproject.eu/lemon#pos ahttp://www.example.org/lexicon#00753137 http://www.monnetproject.eu/lemon#pos vhttp://www.example.org/lexicon#01527311 http://www.monnetproject.eu/lemon#pos vhttp://www.example.org/lexicon#02361703 http://www.monnetproject.eu/lemon#pos v我想要这样:http://www.example.org/lexicon#13796604 http://www.monnetproject.eu/lemon#pos nhttp://www.example.org/lexicon#00603894 http://www.monnetproject.eu/lemon#pos ahttp://www.example.org/lexicon#00753137 http://www.monnetproject.eu/lemon#pos vhttp://www.example.org/lexicon#01527311 http://www.monnetproject.eu/lemon#pos vhttp://www.example.org/lexicon#02361703 http://www.monnetproject.eu/lemon#pos v
3 回答
MM们
TA贡献1886条经验 获得超2个赞
您需要先关闭引号,然后再添加换行符:
'''\
'''+line1+'''
'''+line2+'''
'''+line3+'''
'''+line4+'''
'''+line5+'''
'''+line6+'''
'''+line7+''''''
您逃脱了第一行换行符,但仍在后面添加了4个空格line1。尝试这个:
print("\n".join([line1, line2, line3, line4, line5, line 6, line7]))
烙印99
TA贡献1829条经验 获得超13个赞
采用:
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
和:
print contents,
注意最后的逗号!
智慧大石
TA贡献1946条经验 获得超3个赞
在Python 2.x中,它将是print "Text",
-逗号后不包含任何内容。
在Python 3.x中,它将是print("Text", end="")
-print()
函数的特殊参数。
添加回答
举报
0/150
提交
取消