0

Im trying to reach elements of two lists who both are within a list. And this last list (containing the other two lists) is in a file.

When I print the big list from my file, it looks like: ['[0,1],[2,3]'] and it is called rader.

rader = ['[0,1],[2,3]'] 

I'm trying to print the elements in the big list with the following command: print(rader[0][0]) which should give me the number 0 right? But when I do this I get an error: 'IndexError: list index out of range'.

I think that the problem is that when I open my txt.file with the list in it and read from it (file = open('eddie.txt','r')), it interprets the whole thing as a String! Is that why I cant reach the elements in the list?

I want it like [[0,1],[2,3]] and not ['[0,1],[2,3]'] from the txt.file(I think?) How do I do this?

Here is my code:

from Tv_LABB2 import *


fil = open('edvin.txt','r', encoding = 'UTF-8')

rader = []

rad = fil.readline()

while rad !='':

    rader.append(rad)
    rad = fil.readline() 

fil.close()

fil = open('edvin.txt')

print(rader) (this is == ['[0,1],[2,3]'])

fil.close()



tv1 = TV('VardagsrumsTV',str(rader[0]),str(rader[1]))               

tv2 = TV('KöksTv',str(rader[2]),str(rader[3]))

As you can see Im importing some functions from another file, this code doesn´t run without it, but I think you understand my question!

  • Did you create this file originally? In which case, you could eliminate the problem at its source. – roganjosh Oct 08 '17 at 16:17
  • can you show us what the content of the file looks like? My guess is that a typical line looks like... `[0,1],[2,3]\n` – E. Ducateme Oct 08 '17 at 16:20
  • Yes, I did! When I open my txt-file, it contains the following [[0,1],[2,3]] and not ['[0,1],[2,3]']. Otherwise I would erase the ' ', or what do you mean? - @roganjosh – SimpleMinded Oct 08 '17 at 16:22
  • In which case, you probably want something like the [pickle](https://docs.python.org/2/library/pickle.html) module to write your list to file, rather than a text file. Then this problem will go away. – roganjosh Oct 08 '17 at 16:24
  • I mean: did you _write_ the contents of this file or did someone else _write_ the contents? I'm not asking how it looks in a text editor. – roganjosh Oct 08 '17 at 16:27
  • Aha, now it occurs to me: did you manually write the contents of the file? In which case, my suggestion won't work. Pickle would only work if you programmatically wrote the file. – roganjosh Oct 08 '17 at 16:35

3 Answers3

0

You need to make rader a list of lists not a list of a string

Use rader = [[0,1],[2,3]] not rader = ['[0,1],[2,3]']

  • The question is ambiguous but i think that was illustrating the output of `print` and not how the OP created the list. – roganjosh Oct 08 '17 at 16:19
0

You can convert using ast:

>>> rader = ['[0,1],[2,3]'] 
>>> import ast
>>> r = list(ast.literal_eval(rader[0]))
>>> r
[[0, 1], [2, 3]]
>>> print r[0][0]
0
JacobIRR
  • 6,814
  • 6
  • 30
  • 50
0

Reading a file always produces byte or character strings. If you want Python data structures, you have to make them. Obviously, there are many tools already made to do this: the simplest (but usually inadvisable) is eval. There is a safer variant called ast.literal_eval.

Yet other choices involve changing the on-disk format. You can just use whitespace to separate your values and use things like str.split to make lists, or you can (if your data came from another Python process) use pickle.

Also, a simplification: rader=list(fil) is all you need to read text lines.

Davis Herring
  • 24,173
  • 3
  • 25
  • 55