My code is:
quiz = {}
quiz["questions"]={}
templist = []
def checkint(i):
try:
int(i)
return True
except ValueError:
return False
with open("sample",'r') as f:
for x in f:
x.rstrip()
num,text = x.split(" ",1)
print(text)
num = num[:-1]
if checkint(num):
quiz["questions"][num] = {}
quiz["questions"][num]["question"] = text
templist.append(num)
else:
qn = templist.pop()
quiz["questions"][qn][num] = text
templist.append(qn)
The sample file is:
1. Who is the sower in the parable of wheat and tares?
a. Jesus
b. Peter
c. Angels
d. Satan
When I print the dictionary quiz, the output contains newline \n
at the end of every value.
{'questions': {'1': {'a': 'Jesus\n', 'b': 'Peter\n', 'c': 'Angels\n', 'd': 'Satan\n', 'question': 'Who is the sower in the parable of wheat and tares?\n'}, '2': {'question': '\n'}}}
Why is this so? What should I do to get rid of the \n
?