我正在尝试打开两个文本文件,读取它们,并将内容放在两个单独的临时文件中,以便稍后调用它们。我有以下两个问题:
''
?代码如下:
from os import read
# vars to hold questions and answers from question bank and answer bank in format ready for quiz iterations
test_questions = []
test_answers = []
# placing questions in temp list called test_questions
def read_quest():
bank = open("test_question_bank.txt", "r+")
bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
temp_q = "".join(file_read_q[start:stop])
print(test_questions.append(temp_q))
start += 5
stop += 5
bank.close()
# placing answers in temp list called test_answers
def read_answers():
ans = open("test_correct_ans.txt", "r+")
file_read_a = ans.readlines()
ans.seek(0)
for i in file_read_a:
i = i.strip("\n")
print(test_questions.append(i))
ans.close()
# adding questions
def add_quest():
bank = open("test_question_bank.txt", "a")
correct_ans = open("test_correct_ans.txt", "a")
prompt = input("Please write question: ")
ansa = input("Write answer for choice a): ")
ansb = input("Write answer for choice b): ")
ansc = input("Write answer for choice c): ")
correct_opt = input("Which option is the correct answer? ")
temp_quest = prompt + "\n(a) " + ansa + "\n(b) " + ansb + "\n(c) " + ansc + "\n\n"
temp_quest2 = "".join(temp_quest)
print(bank.write(temp_quest2))
print(correct_ans.write(correct_opt + "\n"))
bank.close()
correct_ans.close()
read_quest()
read_answers()
print(test_questions)
print(test_answers)
#add_quest()
输出如下:
None
None
None
None
None
None
None
None
None
None
None
None
['Where is North?\n(a) s\n(b) e\n(c) n\n\n', 'Where is South?\n(a) s\n(b) e\n(c) n\n\n', '', '', '', '', '', '', '', '', 'c', 'a']
[]
test_question_bank.txt
的内容如下所示:
Where is North?
(a) s
(b) e
(c) n
Where is South?
(a) s
(b) e
(c) n
我的 test_correct_answers.txt
的内容如下所示:
c
a
答案 0 :(得分:4)
您的文件中有很多空行(可能在最后,您没有注意到它们)。您可以通过运行以下命令来清除数据以去除任何空行:
# For each question
# If it is not a blank line
# Add it to the new list
test_questions = [question for question in test_questions if question]
答案 1 :(得分:1)
为什么会出现这么多''
?
这是因为 .readlines()
将每一行作为一个单独的元素读取。所以你的列表看起来像 ['Where is North?\n', '(a) s\n', '(b) e\n', '(c) n\n', '\n', 'Where is South?\n', '(a) s\n', '(b) e\n', '(c) n']
。
现在,您正在执行以下操作:
file_read_q[0:5]
file_read_q[5:10]
因此,理论上,当您一次获取 5 个元素时,您将整个列表分成 2 个切片。但是 for
循环正在迭代列表的元素并且您正在获取其索引甚至不存在于列表中的切片。所以你得到的只是一个空白列表 []
,当它加入时,使 ''
但是如果索引不在列表中,为什么没有引发错误?
为此,您可以refer here
def read_quest():
bank = open("test_question_bank", "r+")
#bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
temp_q = "".join(file_read_q[start:stop]).strip("\n")
if temp_q:
test_questions.append(temp_q)
start += 5
stop += 5
bank.close()
您得到一个空的 test_answers
,因为实际上从未将元素附加到 test_answers
,而是执行 test_questions.append(...)
。 test_answers.append(...)
def read_answers():
ans = open("test_correct_ans.txt", "r+")
file_read_a = ans.readlines()
ans.seek(0)
for i in file_read_a:
i = i.strip("\n")
test_answers.append(i)
ans.close()
答案 2 :(得分:0)
由于您是新手,有几个建议:
test_questions
、test_answers
)之外声明变量,很难调试和控制你的程序。read_questions
比 read_ques
with open('..')
语法。read_quest
中,不要在问题上跨 5 行,而是尝试通过字符串模式提取问题。检查下面的代码:
def read_questions():
with open('test_question_bank.txt') as f:
valid_lines = [x.strip() for x in f.readlines() if x.strip()]
valid_questions = [x for x in valid_lines if not x.startswith('(')]
return valid_questions
def read_answers():
with open('test_correct_answers.txt') as f:
valid_answers = [x.strip() for x in f.readlines() if x.strip()]
return valid_answers
questions = read_questions()
valid_answers = read_answers()
print(questions)
print(valid_answers)
享受 Python。
答案 3 :(得分:0)
哇!感谢您的输入。我当然在这里学到了很多东西。我感到有点尴尬,因为我的第二个问题的答案很简单:我附加到错误的文件中!
至于第一个问题,几个有用的解决方案 - 再次感谢 - 你告诉我问题在于文件是如何迭代的。我想做以下似乎也有效的事情。我只是添加了一个 if 循环来测试起始索引是否超过了它正在迭代的项目的索引。
if start < len(file_read_q):
也许您可以向我反馈此解决方案是否存在潜在问题。
再次感谢大家。
def read_quest():
bank = open("test_question_bank.txt", "r+")
bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
if start < len(file_read_q):
temp_q = "".join(file_read_q[start:stop])
test_questions.append(temp_q)
start += 5
stop += 5
else:
break
bank.close()