Question:
How to pull out different elements from a text file?

Problem:

For part of my programming assignment, I need to write code that takes in a text file name as a command line argument, reads the details of specifically formatted exam questions from the text file and identifies the different features of it. I need it to do this so the program can go on to allow the user to take the exam and answer the questions.


Here is an example of the exam question formatting found in the text file:


Question - Multiple

Select all the animals that are mammals:

Possible Answers: 

A. whale

B. dog

C. fish

D. frog

Expected Answer: A, B

Marks: 2


I need the program to extract the question type (i.e."Multiple"), the question itself, all possible answers, expected answers, and marks awarded for answering correctly.


The text file also has multiple questions inside of it, separated from one another by a blank line. I am unsure of how to get the program to pick apart the details of each question independent from one another.


I am not allowed to use any of the following keywords or built-in functions: for, in (contains()), global, lambda, nonlocal, all(), any(), dir(), eval(), enumerate(), filter(), globals(), locals(), map()


I did try to figure this out for a long while, but I can't seem to understand it. This is currently what I have, although I know it's wildly incorrect. However, it may give some sort of insight into what methods/functions I am allowed to use, so I figured I would add it.


import sys


filename = sys.argv[1]


fobj = open(filename, 'r')


read_lines = fobj.read()


print(read_lines)


while fobj != "":


    if "Question - " in read_lines:

        question_type = read_lines.split("Question - ")

    break


print(question_type)


This doesn't do what it's supposed to at all. I was mostly trying to see if I could just manage to successfully extract the question type and assign it to the question_type variable. However, question_type kept ending up as a list ['', 'Multiple\n'] and I wasn't sure how to fix it.


Solution: 

The split(query) method creates a list by splitting up a string at each occurence of query. Splitting the string "Question - Multiple" at the query "Question - " would indeed give you the list of strings you're getting - the first entry is an empty string '', and the second entry is the rest of the string 'Multiple\n'. The \n is a newline character, which is likely going to be at the end of each line you read in from the file. You're more or less on the right track.


From here, since splitting at "Question - " will always give you a list that looks like the one you got, you can obtain the question type by taking the second entry in the list. Then you can remove extra whitespace characters (including the newline character \n) by using .strip(). In your code this would look like:


if "Question - " in read_lines:

    result = read_lines.split("Question - ")

    question_type_raw = result[1]

    question_type = question_type_raw.strip()

    break


print(question_type)


You can extract the question type in a single line - this is left as an exercise for the reader.


Suggested blogs:

>Quality Testing in AI: Manual to Autonomous Testing

>Top 8 programming languages to used in artificial intelligence coding

>How you can pick a language for AI programming

>Supervised Learning vs Unsupervised Learning- Technical Chamber

>How Artificial Intelligence is Transforming the Gaming Industry

>Sorting the restframework in Django


Ritu Singh

Ritu Singh

Submit
0 Answers