Linebreak无效 - Python

时间:2012-04-01 23:10:57

标签: python list linked-list line-breaks

不知何故,换行符不能正常工作。 这就是我得到的:

Expected:
    O meu u2 2 post
    http://www.yahoo.com
    1 Gosto, 0 Nao gosto
    <BLANKLINE>
    O meu u2 post
    http://www.altavista.com
    1 Gosto, 0 Nao gosto
Got:
    'O meu u2 2 post\nhttp://www.yahoo.com\n1 Gosto, 0 Nao Gosto\n\nO meu u2\nhttp://www.yahoo.com\n1 Gosto, 0 Nao Gosto'

这是函数中使用的代码。 重要的部分应该是str和showRecentComments函数

class Comments():
def __init__(self, u=None, text='', link=None):
    self.u = u
    self.text = text
    self.link = link
    self.topo = None
    self.fim = None

def __str__(self):
    actual = self.topo
    s = ''
    if actual == None:
        return None
    while actual != None:
        if actual.seg == None:
            s += str(actual)
            actual = actual.seg
        else:
            s += str(actual) + '\n' + '\n'
            actual = actual.seg
    return s

def add(self,comment):
    if self.topo == None:
        self.topo = comment
        self.fim = comment
    else:
        comment.seg = self.topo
        self.topo.ant = comment
        self.topo = comment

def remove(self,comment):
    actual = self.topo
    if (self.topo == self.fim) and (self.topo == comment):
        self.topo = None
        self.fim = None
    while actual!=None:
        if actual == comment:
            if self.topo==comment:
                actual.seg.ant = None
                self.topo = actual.seg
            elif self.fim==comment:
                actual.ant.seg = None
                self.fim = actual.ant
            else:
                actual.seg.ant = actual.ant
                actual.ant.seg = actual.seg
            break
        else:
            actual = actual.seg

def countLike(self):
    count = 0
    actual = self.topo
    while actual != None:
        if len(actual.likeList) >= 1:
            count += 1
            actual = actual.seg
        else:
            actual = actual.seg
    return count

def showRecentComments(self,n):
    count = 1
    actual = self.topo
    sC = ''
    if actual == None:
        return None
    while actual != None:
        if count < n:
            sC += str(actual) + '\n' + '\n'
            count += 1
            actual = actual.seg
        elif count == n:
            sC += str(actual)
            count += 1
            actual = actual.seg
        elif count > n:
            break 
    return sC

此致,NelsonGregório

1 个答案:

答案 0 :(得分:2)

看起来你正在查看字符串的表示形式,它会将换行符显示为\n。如果您print或写信给stdout(sys.stdout.write(s))而不是字符串,新行将被扩展。