Hamza's answer is good, but it misses the fact that an answer might be multi-lined.
Better Solution: (assuming text in question is in data.txt file)
import re
with open('data.txt', 'r', encoding='utf8') as file:
data = file.read()
questions = re.split(r'\n\s*\n', data) #splits the questions into a list assuming there is no empty lines inside each question
final_questions = []
for question in questions:
if question != None and '(a)' in question: #extra check to make sure that this a question
statement = re.findall(r'[^(]+', question)[0].replace('\n', ' ').rstrip()
option_a = re.findall(r'\(a\)[^(]+', question)[0].replace('\n', ' ').rstrip()
option_b = re.findall(r'\(b\)[^(]+', question)[0].replace('\n', ' ').rstrip()
option_c = re.findall(r'\(c\)[^(]+', question)[0].replace('\n', ' ').rstrip()
option_d = re.findall(r'\(d\)[^(]+', question)[0].replace('\n', ' ').rstrip()
final_questions.append({
'statement': statement.rstrip(),
'options': [option_a, option_b, option_c, option_d]
})
print(final_questions)
Output:
[
{
"statement":"17. If you go on increasing the stretching force on a wire in a guitar, its frequency.",
"options":[
"(a) increases",
"(b) decreases",
"(c) remains unchanged",
"(d) None of these"
]
},
{
"statement":"18. A vibrating body",
"options":[
"(a) will always produce sound",
"(b) may or may not produce sound if the amplitude of vibration is low",
"(c) will produce sound which depends upon frequency",
"(d) None of these"
]
},
{
"statement":"19. The wavelength of infrasonics in air is of the order of",
"options":[
"(a) 100 m",
"(b) 101 m",
"(c) 10–1 m",
"(d) 10–2 m"
]
}
]
Note:: There should be at least one empty line between each question