recover a partly destroyed pickled structure

Mike C. Fletcher mcfletch at
Sat Sep 25 19:28:46 CEST 2004

Each line you quote as "interesting" is an item in a list, denoted by 
the line starting with 'a', after that is the type notation ('F', for 
float), so if that's all the data you need, you can just do:

values = []
for line in lines:
    line = line.strip()
    assert line.startswith( 'aF' ), """Line %r doesn't start with 
    value = float(line[2:])
    values.append( value )

doSomethingWithValues( values )

You can see the formats quite simply by using pickle at the command line:

In [3]: pickle.dumps( [ 2.0, 3.0] )
Out[3]: '(lp0\nF2.0\naF3.0\na.'


Maurizio Berlusconi wrote:

>Hi all
>I have (my mistake) overwritten the beginning of a file where a long
>dictionary was pickled with another (pickled and much smaller) dictionary.
>Since only 10% or so of the original and important data was overwritten, I
>would be happy if I could somehow recover at least the part that I can
>still see.

>ss. #after that line starts the data I need
>....# all data is here until the end
># end of old important data
>Can anybody give an indication on how to do that? I tried to understand
>how pickle puts these numbers on the different fields and remove the
>unimportant beginning of the file to replace it with something that would
>allow to unpickle the data, but did not really succeed. Read the
>source (and understand it) is far beyond my python skills...
>Any hint or pointer would be appreciated.

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list