Python Forum

Full Version: Using pickle.dump
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am new to Python and i am learning it with a book trying all the exemple there.
Now i use the first time the module pickle, and tehre was an example to something simple into a file on my PC. the code was the following :
tel = [('Tim','123'),('Jenny','456'),('Max','789')]
import pickle
data = file('myfile.txt','w')
pickle.dump(tel,data)
data.close()
the text file was generated,but with some strage random letters/numbers, this is part of the result :
(lp0
(S'Tim'
p1
S'123'
p2
tp3
a(S'Jenny'

i also tried a simple string (tel = 'abc'), same result.

What is wrong in this code ?

thanks in advance
you want open() not file, and 'wb' not 'w' for write bytes. And you may a well use the with keyword which is more pythonic.
with open('myfile.pkl', 'wb') as f:
    pickle.dump(tel, f)