为什么我的python不能打印在同一行上?

2024-05-14 03:43:40 发布

您现在位置:Python中文网/ 问答频道 /正文

question = "question:"
# QUESTION
f = open('dopeit.rtf', encoding="utf8", errors='ignore')

line = f.readline()

while line:

    if question in line:
        newline = line.replace('Question: ', '"')
        print(newline + '"', end=",")

    # use realine() to read next line
    line = f.readline()
f.close()

输出是这样的

","Who directed Star Wars?
","Who was the only non Jedi in the original Star Wars trilogy to use a lightsaber?
","What kind of flower was enchanted and dying in Beauty and the Beast?
","Which is the longest movie ever made?

我希望它是这样的:

"Who directed Star Wars?","Who was the only non Jedi in the original Star Wars trilogy to use a lightsaber?","What kind of flower was enchanted and dying in Beauty and the Beast?","Which is the longest movie ever made?

那么我怎样才能做出这些改变呢?我尝试使用“end”命令,但它似乎仍能将其带到下一行?我做错什么了吗


Tags: andthetoinreadlineuselinenewline