印刷线在Python中表现得很奇怪

时间:2013-06-26 14:33:42

标签: python loops

我想在几个循环中打印出一堆行,我想找到一种不使用\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.line6

def 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.line7

def compose_contents(self, line1, line2, line3, line4, line5, line6, line7):
    self.contents = '''\
    '''+line1+'''
    '''+line2+'''
    '''+line3+'''
    '''+line4+'''
    '''+line5+'''
    '''+line6+'''
    '''+line7+''''''
    return self.contents

def print_lines(self, contents):
    print (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

我想这样:

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

帮助非常棒,谢谢

3 个答案:

答案 0 :(得分:2)

在添加换行符之前,您需要关闭引号:

'''\
    '''+line1+'''
    '''+line2+'''
    '''+line3+'''
    '''+line4+'''
    '''+line5+'''
    '''+line6+'''
    '''+line7+''''''

您逃脱了第一个换行符,但它仍然在line1后面添加了4个空格。试试这个:

print("\n".join([line1, line2, line3, line4, line5, line 6, line7]))

答案 1 :(得分:2)

使用:

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,

注意最后的逗号!

答案 2 :(得分:1)

在Python 2.x中,它将是print "Text", - 逗号之后没有任何内容。

在Python 3.x中它将是print("Text", end="") - 只是print()函数的特殊参数。