List return by open() umlike normal python list

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.

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

1 Like

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.

you run in python IDLE?

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

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()

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