从\ .txt文件读取文本时,在我的python代码末尾编写“ \ n”

时间:2019-11-12 20:47:22

标签: python

我尝试了许多方法来解决此问题,例如rstrip,strip和replace,但仍然无法正常工作。 我正在控制台中创建一个小型问答游戏。我的代码如下:

from random import seed
from random import randint
import time
from time import sleep 
from os import system, name
import os

questionNumber = randint(1, 2) # Choses a number
questionfile = "Questions/Q" + str(questionNumber) + (".txt") # File being randomly chosen
file = open(questionfile, 'r') # Question Selected

question = file.readlines(1) # First line being read

def clear(): # Clears the console
    if name == 'nt': 
        _ = system('cls') 
    else: 
        _ = system('clear')

input("Click 'ENTER' to get started!") 
clear()

print(question)

我有两个文件,分别为Q1.txtQ2.txt。这些都在文件的文件夹中:

Q1.txt:

What is the capital of Australia?
Sydney
Melbourne
Adelaide
Perth

Q2.txt:

When did WWI start?
1914
1932
1918
1915

当我运行程序时,它将打印以下内容:

['When did WWI start?\n']

['What is the capital of Australia?\n']

我正试图摆脱'\n'和方括号。任何帮助将不胜感激。

0 个答案:

没有答案