to convert a MCQ in the required format - python
<p>I am trying to convert MCQ which is as follows:</p>

<pre><code>Which will legally declare, construct, and initialize an array?
A. int [] myList = {"1", "2", "3"};
B. int [] myList = (5, 8, 2);
C. int myList [] [] = {4,9,7,0};
D. int myList [] = {4, 3, 7};
ANSWER: D
</code></pre>

<p>into the required format which is as mentioned below:</p>

<pre><code>Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
*int myList [] = {4, 3, 7};
</code></pre>

<p>The logic I'm trying is as follows:</p>

<pre><code>1. Open the text file and trying to fetch the line number of "ANSWER: D"
2. Open the file again and go to that line number
3. write a for loop which will iterate max 5 times till it find the match 
   "D." is found.
4.Once the match is found replace it with '*'
</code></pre>

<p>Below is the code I tried:</p>

<pre><code>import re

ans = []
line_no = []

class Options:
  def __init__(self, ans1, num1):
    self.a = ans1
    self.n = num1
    #print(self.a, self.n)


pattern = 'ANSWER: [A-Z]' # to fetch the answer of each question
r = re.compile(pattern)

pattern1 = '[A-F]\.\s'
re1 = re.compile(pattern1)



with open (r"C:\Users\dhvani\Desktop\test.txt", "r") as f:
 for num, line in enumerate(f, 1):
    d = r.findall(line)
    if(d):
        l = d[0].split(":")
        m = l[1].split(" ")
        m = m[1] + "."
        ans.append(m)
        line_no.append(num)

x = Options(ans, line_no)
print(x.a, x.n)



with open (r"C:\Users\dhvani\Desktop\test.txt", "r") as f:
  for i, j in enumerate(ans):
     j1 = j[0]
     z = f.readlines()[j1 - 1]
     print(z)
     for n in range(j1 - 1, j1 - 7, -1):
         value = f.readline()
         value1 = re1.findall(value)

         if value1:
             if value1 == i:
                 value.sub('[A-F]\.\s', '*', value)
         break;
</code></pre>

<p>I am able to fetch the line number of "ANSWER: D" and store 'D.' and its corresponding line number in two different lists.</p>

<p>Then later steps are unsuccessful.</p>

<p>Any help will be greatly appreciated.
I am new to Python.</p>

<p>You could use the following code: reading from a file, doing required modifications and writing them into a new text file.</p>

<pre><code>import re

with open("your_filename_here", "r") as fp:
    string = fp.read()
    f1 = open(r"your_filename_here", "w")

    rx = re.compile(r'''
                (?!^[A-E]\.)
                (?P&lt;question&gt;.+[\n\r])
                (?P&lt;choices&gt;[\s\S]+?)
                ^ANSWER:\ (?P&lt;answer&gt;[A-E])
                ''', re.MULTILINE | re.VERBOSE)

    rq = re.compile(r'^[A-E]\.\ (?P&lt;choice&gt;.+)')

    for match in rx.finditer(string):

        def repl(line, answer):
            if line.startswith(answer):
                line = rq.sub(r"*\1", line)
            else:
                line = rq.sub(r"\1", line)
            return line

        lines = [repl(line, match.group('answer')) 
                for line in match.group('choices').split("\n") 
                if line]

        block = match.group('question') + "\n".join(lines)
        #print(block)
        f1.write(block + "\n\n")
</code></pre>

<p>This splits your blocks into parts of question, choices and the answer and analyzes them afterwards. See <a href="http://ideone.com/Mvp0PE" rel="nofollow"><strong>a demo on ideone.com</strong></a>.</p>

python regex file