打印字符串没有完全停止进入Python的下一行

时间:2013-04-27 21:25:18

标签: python string

我已经很好地浏览了网站和网页,因为它非常具体,所以无法找到如何做到这一点,而且我已经尝试了一切!

我有一个执行搜索的功能,我正在尝试打印列表中特定位置的纯文本和字符串。

这是我的代码:

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 个答案:

答案 0 :(得分:1)

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

str(journals[position]).rstrip()

str(journals[position])