List return by open() umlike normal python list


#1

I have a list, artists[] where i could do, say, a=artists[7] [5]..., & print(a), but when I save it with
outfile=open('artists.txt', 'w') & reload it with infile=open('artist.txt','r') the returned list doesn't behave like the original.


#2

can you provide the full code you used so we can have a look?


#3

Like this:

artists=[]

artists=[['john', 'smith',guitar'],['joe','santos','bass'],['sam', 'natividad', keyboard']]
With this i could do a=artists[1] [2] & this would print, 'a=bass' as to be expected.

Then I saved this with outfile=open('artists.txt', 'w'). I then reloaded it again with infile=open('artists.txt', 'r'),
& guys=infile.read() but the returned list (python books say guys is a list) is different from the orignal that i couldn't do,say, print(artist[1] [2]) like before.


#4

you run in python IDLE?

please just post full code you run without any explanation in it


#5

Yes,IDLE,python 3.5.1

artist=[]

def main():
artist=[ ['john','smith', 'guitar'], ['joe', 'santos', 'bass'],['sam', 'natividad', 'keyboard'] ]

a=artist[1] [2]
print (a)

#### save this list
outfile=open('artist.txt', 'w')
outfile.write(str(artist))

main()


#6

the problem is that you can write a list to file (well, it must be possible with libraries)

so you convert to string. When reading from the file, use eval() to interpreter the string as python code to get back to your list

or google how you can save list to file