【问题标题】:to convert a MCQ in the required format - python以所需格式转换 MCQ - python
【发布时间】:2016-09-10 06:19:27
【问题描述】:

我正在尝试转换 MCQ,如下所示:

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

转换成如下所述的所需格式:

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};

我正在尝试的逻辑如下:

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 '*'

下面是我试过的代码:

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;

我能够获取“ANSWER: D”的行号并存储“D”。及其对应的行号在两个不同的列表中。

然后后面的步骤不成功。

任何帮助将不胜感激。 我是 Python 新手。

【问题讨论】:

    标签: python regex file


    【解决方案1】:

    您可以使用以下代码:从文件中读取,进行必要的修改并将它们写入新的文本文件。

    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<question>.+[\n\r])
                    (?P<choices>[\s\S]+?)
                    ^ANSWER:\ (?P<answer>[A-E])
                    ''', re.MULTILINE | re.VERBOSE)
    
        rq = re.compile(r'^[A-E]\.\ (?P<choice>.+)')
    
        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")
    

    这会将您的块分成问题、选择和答案的部分,然后对其进行分析。见a demo on ideone.com

    【讨论】:

    • 非常感谢先生。输入是一个文本文件。您能帮我在哪一行代码中插入文件打开命令吗?
    • 我应该为 1600 个 MCQ 执行上述过程。
    • 完成在文本文件中读取和写入所需格式。在 string = "your-string" 之前添加两行 open (r"your-file-path", "r") as f: string = f.read() f1 = open(r"your-file-path", "w") 并在最后; f1.write(block + "\n\n") 谢谢先生的大力帮助
    • @DhvaniShah:使用with open...查看更新的abswer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多