2013-04-27 15 views
1

我已经很好的了解了网站和网页,并且找不到如何做到这一点,因为它相当具体,而且我尝试了一切!打印字符串没有完全停留在Python中的下一行

我有一个函数执行搜索,我试图打印列表中特定位置的纯文本和字符串。

这是我的代码:

def search_authors(the_author, authors): 

    position = 0 

    foundAuthor = False 

    for i in authors: 
     if the_author in i: 
       print((the_author) + ". (" + str(years[position]) + "). " + str(titles[position]) + ". " + str(journals[position]) + ". " ) 
       foundAuthor == True 

     # 
     #  for some reason this is prnting a dot on the next line, not just after the last entry 
     # 

     elif position == len(authors) and foundAuthor == False: 
       print("not here") 
     position += 1 

这是打印时输出:

Anonymous. (1993). Technical correspondance - Random Number Generators. Communications  of the ACM 
. 
Anonymous. (1992). Research Directions in Virtual Environments (NSF Invitational Workshop). Computer Graphics 
. 
Anonymous. (1998). "Exploring sequential data: Commentary on Bowers, Jentsch, Salas, and Braun (1998)". Human Factors 
. 

我无法弄清楚如何阻止每个搜索结果的最后一个句号去到下一行, 任何帮助将不胜感激。

回答

1

在期刊列表的每个元素中都可能有换行符。尝试取代:

str(journals[position]).rstrip() 

str(journals[position]) 
+0

这就是完美工作,非常感谢您的快速反应闪电! – 2013-04-27 21:34:47

+0

如果它的工作,我会很感激,如果你可以标记答案为“接受”,谢谢! – 2013-04-27 21:40:47

相关问题