pickled

hi,
I'm having troublepickling and saving a list to a file,for example:
alist'["a","b","c"]
picklist=pickle.dump(alist)
file=open('file","a")
file.write(picklist)
file.close
If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,
thanks
nevle

Comments

  • : hi,
    : I'm having troublepickling and saving a list to a file,for example:
    : alist'["a","b","c"]
    : picklist=pickle.dump(alist)
    : file=open('file","a")
    : file.write(picklist)
    : file.close
    : If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,

    As for the pickle file reading down instead of across, that is just how pickle works. You shouldn't mess with pickle files anyways unless you intimately understand the format python uses to pickle objects. I believe that pickling isn't built to store multiple objects in a single file (unless perhaps they are constituents of some larger object that itself is being pickled). You should perhaps look at the "shelve" module.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : : hi,
    : : I'm having troublepickling and saving a list to a file,for example:
    : : alist'["a","b","c"]
    : : picklist=pickle.dump(alist)
    : : file=open('file","a")
    : : file.write(picklist)
    : : file.close
    : : If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,
    :
    : As for the pickle file reading down instead of across, that is just how pickle works. You shouldn't mess with pickle files anyways unless you intimately understand the format python uses to pickle objects. I believe that pickling isn't built to store multiple objects in a single file (unless perhaps they are constituents of some larger object that itself is being pickled). You should perhaps look at the "shelve" module.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    OK, thanks infidel I thought the shelve module would probably do what I wanted but decided to have look at pickling first, and I was unsure if I was getting the syntax right.
    nevle


  • : hi,
    : I'm having troublepickling and saving a list to a file,for example:
    : alist'["a","b","c"]
    : picklist=pickle.dump(alist)
    : file=open('file","a")
    : file.write(picklist)
    : file.close
    : If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,
    : thanks
    : nevle
    :
    :

    Hi nevle,

    I find that pickling works best if all the data in enclosed in square brackets.

    if you initially assign =>
    alist = []

    You can then add the 1st list =>
    alist.append(['a','b','c'])

    and add another a 2nd list =>
    alist.append(['d','e','f'])

    Hence, alist is now => [['a','b','c']['d','e','f']]
    and to read individual list elements =>

    alist[0][0] => 'a'
    alist[0][1] => 'b'
    alist[0][2] => 'c'
    alist[1][0] => 'd' etc

    If the above was pickle.dumped to file, you would later be able to pickle.load the data back to alist at a later stage and add more lists as necessary using the method above.

    With this method though, you cannot append it to the file as such, you instead append to the variable, alist and rewrite the entire alist with new lists back to the file. So, with really large data files, this could become inefficient.

    I hope this has been of some help.

    Regards
    [HR]canoeman :-)

    [purple][italic]"Win some, lose some, can't lose them all!"[/italic] ~ [b]David Helfgott[/b][purple]
    [HR]


  • : : hi,
    : : I'm having troublepickling and saving a list to a file,for example:
    : : alist'["a","b","c"]
    : : picklist=pickle.dump(alist)
    : : file=open('file","a")
    : : file.write(picklist)
    : : file.close
    : : If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,
    : : thanks
    : : nevle
    : :
    : :
    :
    : Hi nevle,
    :
    : I find that pickling works best if all the data in enclosed in square brackets.
    :
    : if you initially assign =>
    : alist = []
    :
    : You can then add the 1st list =>
    : alist.append(['a','b','c'])
    :
    : and add another a 2nd list =>
    : alist.append(['d','e','f'])
    :
    : Hence, alist is now => [['a','b','c']['d','e','f']]
    : and to read individual list elements =>
    :
    : alist[0][0] => 'a'
    : alist[0][1] => 'b'
    : alist[0][2] => 'c'
    : alist[1][0] => 'd' etc
    :
    : If the above was pickle.dumped to file, you would later be able to pickle.load the data back to alist at a later stage and add more lists as necessary using the method above.
    :
    : With this method though, you cannot append it to the file as such, you instead append to the variable, alist and rewrite the entire alist with new lists back to the file. So, with really large data files, this could become inefficient.
    :
    : I hope this has been of some help.
    :
    : Regards
    : [HR]canoeman :-)
    :
    : [purple][italic]"Win some, lose some, can't lose them all!"[/italic] ~ [b]David Helfgott[/b][purple]
    : [HR]
    :
    :
    :

    nevle


  • : : hi,
    : : I'm having troublepickling and saving a list to a file,for example:
    : : alist'["a","b","c"]
    : : picklist=pickle.dump(alist)
    : : file=open('file","a")
    : : file.write(picklist)
    : : file.close
    : : If I add another list to the file ,when I try to file.read() I can only retrieve the first entry.Also when I view the file the data is not written across the page but down it.Any suggestions would be appreciated ,
    : : thanks
    : : nevle
    : :
    : :
    :
    : Hi nevle,
    :
    : I find that pickling works best if all the data in enclosed in square brackets.
    :
    : if you initially assign =>
    : alist = []
    :
    : You can then add the 1st list =>
    : alist.append(['a','b','c'])
    :
    : and add another a 2nd list =>
    : alist.append(['d','e','f'])
    :
    : Hence, alist is now => [['a','b','c']['d','e','f']]
    : and to read individual list elements =>
    :
    : alist[0][0] => 'a'
    : alist[0][1] => 'b'
    : alist[0][2] => 'c'
    : alist[1][0] => 'd' etc
    :
    : If the above was pickle.dumped to file, you would later be able to pickle.load the data back to alist at a later stage and add more lists as necessary using the method above.
    :
    : With this method though, you cannot append it to the file as such, you instead append to the variable, alist and rewrite the entire alist with new lists back to the file. So, with really large data files, this could become inefficient.
    :
    : I hope this has been of some help.
    :
    : Regards
    : [HR]canoeman :-)
    :
    :canoeman,
    thanks for the suggestion,you are saying to make a list of lists and then pickling that,something that I should of thought of!I had a go at shelving the data instead and found this to work very well also.
    thanks for the reply,
    :

    nevle


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories