简体   繁体   English

打印的行在Python中表现奇怪

[英]Printed lines acting strange in Python

I am tring to print out a load of lines in a few loops and I want to find a way of printing out the lines without using \\n as that adds an empty line after each loop is completed. 我正试图在几个循环中打印出一行的行,并且我想找到一种无需使用\\n即可打印出行的方法,因为这样会在每个循环完成后添加一个空行。 A sample of the code i have is as follows: 我拥有的代码示例如下:

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)

When i print these this is what happens: 当我打印这些时,会发生以下情况:

        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

And i would like it like 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

Help would be great thanks 帮助将非常感谢

You need to close your quotes before you add newlines: 添加换行符之前,您需要先关闭引号:

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

You escaped the first newline, but it is still adding 4 spaces behind line1 . 您逃脱了第一条换行符,但仍在line1后面添加了4个空格。 Try this: 尝试这个:

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

use: 采用:

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: 和:

print contents,

Note the comma at the end! 注意最后的逗号!

In Python 2.x it will be print "Text", - nothing after comma. 在Python 2.x中,它将print "Text",逗号后不显示任何内容。

In Python 3.x it will be print("Text", end="") - just special argument to print() function. 在Python 3.x中,它将为print("Text", end="") -只是print()函数的特殊参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM