recover a partly destroyed pickled structure

Discussion in 'Python' started by Maurizio Berlusconi, Sep 25, 2004.

  1. 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.

    The situation is now the following. If I open the file in vi I can see :

    (dp0
    S'011'
    p1
    (dp2
    S'profile'
    p3
    (lp4
    F3.125
    aF5.625
    aF8.59375
    ....
    #uninteresting data
    ....
    p460
    sg36
    g6
    sg37
    S'0:00'
    p461
    sg39
    (lp462
    sg41
    g6
    ss. #after that line starts the data I need
    aF10.0
    aF10.3125
    aF8.125
    aF4.84375
    aF4.21875
    aF5.9375
    .....# all data is here until the end
    g4
    sg215
    S'12:26'
    p3376
    ss.
    # 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.

    Regards,

    Maurizio
     
    Maurizio Berlusconi, Sep 25, 2004
    #1
    1. Advertising

  2. 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
    aF"""%(line,)
    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.'

    HTH,
    Mike

    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
    >aF10.0
    >aF10.3125
    >aF8.125
    >aF4.84375
    >aF4.21875
    >aF5.9375
    >....# all data is here until the end
    >g4
    >sg215
    >S'12:26'
    >p3376
    >ss.
    ># 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.
    >
    >Regards,
    >
    >Maurizio
    >
    >


    --
    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
     
    Mike C. Fletcher, Sep 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Fitzsimons

    Partly consistent web pages - How ?

    John Fitzsimons, Jul 23, 2004, in forum: HTML
    Replies:
    3
    Views:
    410
    Karl Groves
    Jul 23, 2004
  2. Harlan Messinger
    Replies:
    1
    Views:
    385
  3. zaley
    Replies:
    18
    Views:
    463
    zaley
    Feb 25, 2008
  4. process

    Inheritance but only partly?

    process, Oct 2, 2008, in forum: Python
    Replies:
    12
    Views:
    494
    George Sakkis
    Oct 5, 2008
  5. Robert Fendt
    Replies:
    0
    Views:
    354
    Robert Fendt
    Feb 14, 2010
Loading...

Share This Page