-1

I have a text file which contains the questions that I want to be asked in a quiz that I am trying to code in Python. I want these questions to appear as follows:

Multiple choice question?
(a) Option 1
(b) Option 2
(c) Option 3

As such, the question string is written out as follows:

"1. Multiple choice question?\n(a) Option 1\a(b) Option 2\n(c) Option 3\n\n"

These strings are stored in a text file, so that I can change them or add to them

I then use the following code to import these strings into a list in Python:

with open("questions.txt") as file_in:
    questions = []
    for question in file_in:
        questions.append(question)    

However, when I then:

print(questions)

I get:

['1. What is seven squared?\\n(a) 47\\n(b) 49\\n(c) 59\\n\\n\n', '2. What is the square root of 121?\\n(a) Nine\\n(b) Ten\\n(c) Eleven\\n\\n\n', '3. If a cube has a volume of 1000 cm^2, how long is each side?\\n(a) 6cm \\n(b) 8cm\\n(c) 10cm\\n\\n\n', '4. What is 347 minus 298?\\n(a) 59\\n(b) 49\\n(c) 69\\n\\n\n', '5. What is 22 x 11?\\n(a) 231\\n(b) 131\\n(c) 341\\n\\n\n']

i.e. extra \ characters have been added during the import, which means when I use these strings in my quiz, I see all of the \n code which is supposed to be formatting the question. I just want to import the strings exactly as they are in the text file, without the 'treatment' of \n resulting in

"\\n"

(two backslash ns)

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 3
    The strings *are* exactly as they are in your text file, with the literal backslash characters `\ ` followed by `n` characters. It's just that Python prints a backslash in a string as `\\ ` in this context so you know it's a literal backslash and not an escape sequence for a newline character. – kaya3 Oct 06 '21 at 21:39
  • It's supposed to be an escape sequence for a newline character though. When I use this string in my quiz, I want Python to interpret the \n characters to format the question across multiple lines. What happens is that it's all on a single line, and with the question containing \n (because of the additional \ that has been added) – Ben Darvill Oct 06 '21 at 21:41
  • 1
    Well, your question says you want the strings "exactly as they are in the text file", and in the text file you have backslash characters followed by letter n's. If you wanted newlines then you could put newlines in your text file instead; or otherwise, if you want to *convert* the literal escape sequence `\n` from the text file into a newline in Python, then you don't want it to be exactly as it is in the text file. – kaya3 Oct 06 '21 at 21:42
  • i.e. how do I prevent Python from - as you put it - 'printing a backslash in a string as '''\\''' – Ben Darvill Oct 06 '21 at 21:44
  • Escape sequences are only processed in string literals, not strings read from a file. – Barmar Oct 06 '21 at 21:44
  • You shouldn't put `\n` in the file, just put real line breaks. – Barmar Oct 06 '21 at 21:44
  • If you print the string instead of printing the list which contains the string, then you'll get the single backslash exactly as it is in the text file. – kaya3 Oct 06 '21 at 21:45
  • You are printing a list, but if you printed each of the list members separately, it would display it as what you loaded. When you print the representation of a list, it will probably print the representation of the strings contained in it. – Ben Y Oct 06 '21 at 21:47
  • 2
    @kaya3 I'm appreciating your help here, but not seeing the solution still. The code that I am using to import the text is putting each line of the text file into my list as a single string list element (which is what I want). If I do as you suggest and put newlines in the text file then I will end up with the Question and each of the multiple choices as separate elements in the list, rather than as a single string – Ben Darvill Oct 06 '21 at 21:48
  • The solution is for you to convert each `\n` into a newline character yourself, since Python won't do that for you. – kaya3 Oct 06 '21 at 21:49
  • Would you like to have each question with options as a separate strig? – Roxy Oct 06 '21 at 21:52
  • I'm trying to import the questions *with their answer options* into a list ["Question 1 (a) answer 1 (b) answer 2 (c) answer 3"] If I add line breaks in the text file, won't I end up with: ["Question 1", "(a) answer 1", "(b) answer 2", "(c) answer 3" ] – Ben Darvill Oct 06 '21 at 21:53
  • Sidenote: to create `questions` more easily and remove the extra trailing newline: `questions = file_in.splitlines()` – wjandrea Oct 06 '21 at 22:06

1 Answers1

1

Firstly, read Why do backslashes appear twice? In short, Python uses backslashes to start escape sequences, so '\n' represents a newline and '\\' represents a backslash itself.


Now, I think the main problem here is conceptual. Reading a file is not the same as importing it. Reading means treating it as text, while importing means treating it as code.

I think the best solution here is a middle ground: structured data* like JSON or CSV. Here's how to do it in JSON:

questions.json

[
  "1. Multiple choice question?\n(a) Option 1\n(b) Option 2\n(c) Option 3\n\n"
]

tmp.py

import json

with open('questions.json') as f:
    questions = json.load(f)  # Parse JSON into data

print(questions)

Output

['1. Multiple choice question?\n(a) Option 1\n(b) Option 2\n(c) Option 3\n\n']

* I'm not sure this exactly the right term for it

wjandrea
  • 28,235
  • 9
  • 60
  • 81