-2

I want to save the options from a file in a list like:

options = [
    ["ala", "bala", "ma", "sc"],
    ["fg", "ada", "aas","asd"],
]

This options are for a quiz. For questions I was able to take the text line by line from the file, but for options this is not working:

with open('Options.txt', 'r') as k:
    options = k.readlines()

What should I do?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    What is the content of `Options.txt` file ? – Laurent H. Jun 05 '18 at 15:59
  • Welcome to your first question on StackOverflow. However, you need to include enough information in your question for us to help you. Please read and follow [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton Jun 05 '18 at 16:04
  • You can use eval, like this: `with open('Options.txt', 'r') as k: options = eval(k.read()[10:]) ` But there are probably better ways. PS: The slice [10:] is because I thought that "options = " was included in the txt file. Feel free to remove it if that was not the case. – Kostas Mouratidis Jun 05 '18 at 16:05
  • @KostasMouratidis [why-is-using-eval-a-bad-practice](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Patrick Artner Jun 05 '18 at 16:06
  • "joystick", "imprimanta", "modem", "scanner" "Power Computer", "Personal Computer", "Professional Computer", "Pret de Cost" "Scanner", "plotter", "Videoproiector", "Touchscreen" "Sustine componentele interne", "Alimenteaza componentele interne", "Transmite comenzi componentelor, fiind creierul calculatorului", "Foloseste la pastrarea informatiilor pe termen lung" "1+1=10", "1+1+1=11", "10+11=101", "11+11=100" – AlexandraDy9 Jun 05 '18 at 16:16
  • This is in Options.txt – AlexandraDy9 Jun 05 '18 at 16:17
  • @DianaAlexandraNica Edit your question - add the date there. Best case: inside a code block so newlines are kept. – Patrick Artner Jun 05 '18 at 16:18

3 Answers3

1

You can store each set of options as 1 line, comma-separated:

# create a file
filename = "Options.txt"
with open(filename, 'w') as file:
    file.write("a,b,c,d\n")
    file.write("4,7,8,9\n")
    file.write("27,k,l,pp\n")

Your file would look like:

a,b,c,d
4,7,8,9
27,k,l,pp

You can read in this file by:

# read file in
options =[]
with open(filename, 'r') as file:
    for line in file:
        options.append(line.strip().split(","))

print(options) 

Output:

[['a', 'b', 'c', 'd'], ['4', '7', '8', '9'], ['27', 'k', 'l', 'pp']]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

If your Options.txt file has data as follows:

ala bala ma sc 
fg ada aas asd
---
--- so on

Do this:

with open('Options.txt', 'r') as k:
lst = []
file_line = k.readline()
while file_line:
    file_line_lst = list(file_line.strip().split())
    lst.append(file_line_lst)
    file_line = k.readline()
PKBEST
  • 321
  • 2
  • 13
0

The issue your facing here is this is more easily handled by pulling the contents in as a pair of lists data structures rather than a string object. The simplest way is to rename your options.txt file options.py and assuming this code runs in the same directory, it could look like this.

In [4]: import options as o  

In [5]: gennie = ((o.options[0]+o.options[1]))  

In [6]: type(gennie)  
Out[6]: list  

In [7]: gennie  
Out[7]: ['ala', 'bala', 'ma', 'sc', 'fg', 'ada', 'aas', 'asd']  

In [8]: g = iter(gennie)  

In [9]: while True:  
   ...:     try:  
   ...:         print(g.__next__())  
   ...:     except StopIteration:  
   ...:         break  
   ...:     

Output:
ala
bala
ma
sc
fg
ada
aas
asd