Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using pickle.dump
#1
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
Reply
#2
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)
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 274 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,541 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,607 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,721 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 2,977 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  how json dump Japanese yunkai 2 8,004 Mar-17-2020, 07:06 AM
Last Post: vishalhule
  Python html herf to json dump help paulfearn100 0 1,998 Mar-03-2020, 09:16 PM
Last Post: paulfearn100
  print python json dump onto multiple lines lhailey 2 19,661 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  computing entropy using pickle files baran01 2 2,374 Dec-30-2019, 09:45 PM
Last Post: micseydel
  creating hex dump modifying program ensoniq 3 2,594 Oct-14-2019, 08:21 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020