换行符不工作-Python
不知何故,换行符没有按应有的方式工作。 这就是我得到的:换行符不工作-Python,python,list,linked-list,line-breaks,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
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'
预期:
O meu u2 2岗位
http://www.yahoo.com
1戈斯托,0纳奥戈斯托
O meu u2岗位
http://www.altavista.com
1戈斯托,0纳奥戈斯托
得到了:
“O meu u2 2岗位\nhttp://www.yahoo.com\n1 Gosto,0 Nao Gosto\n\n无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
class注释():
定义初始化(self,u=None,text='',link=None):
self.u=u
self.text=文本
self.link=link
self.topo=无
self.fim=无
定义(自我):
实际=自我拓扑
s=“”
如果实际==无:
一无所获
而实际的!=无:
如果actual.seg==无:
s+=str(实际值)
实际值=实际值.seg
其他:
s+=str(实际值)+'\n'+'\n'
实际值=实际值.seg
返回s
def添加(自我,注释):
如果self.topo==无:
self.topo=注释
self.fim=注释
其他:
comment.seg=self.topo
self.topo.ant=注释
self.topo=注释
def删除(自我,注释):
实际=自我拓扑
如果(self.topo==self.fim)和(self.topo==注释):
self.topo=无
self.fim=无
虽然是真的=无:
如果实际==注释:
如果self.topo==注释:
actual.seg.ant=无
self.topo=actual.seg
elif self.fim==注释:
actual.ant.seg=无
self.fim=actual.ant
其他:
actual.seg.ant=actual.ant
actual.ant.seg=actual.seg
打破
其他:
实际值=实际值.seg
def countLike(self):
计数=0
实际=自我拓扑
而实际的!=无:
如果len(实际似然列表)>=1:
计数+=1
实际值=实际值.seg
其他:
实际值=实际值.seg
返回计数
def showRecentComments(自我,n):
计数=1
实际=自我拓扑
sC=“”
如果实际==无:
一无所获
而实际的!=无:
如果计数n:
打破
返回sC
注意,Nelson Gregório看起来您正在查看字符串的表示形式,它将显示换行符
\n
。如果您打印
或写入例如stdout(sys.stdout.write)
)字符串,换行符将展开。“这就是我得到的:”。。那么,您该怎么办?您不应该从\uuuu str\uuuu
返回None
,而是返回'
。str函数仍然有效。showRecentComments是否也应该这样做?------打印而不是返回作品确实。我想可以。return
仍然有意义,但您希望打印返回的字符串。e、 g.如果你有c
作为你的Comments
类的一个实例,那么你可以执行print c
,这将使用\uu str\uuu
获得所述实例的字符串表示并打印它。