![]() |
|
|
|
#1 |
|
Hi there,
I have to make a small database using cPickle. I'm having troubles trying to read in the information if it's more than one line. I'm pretty sure it's in the line "for line in stuff:" Can anyone help me out? Basically the end result is wanting it to look something like what is down below when list is typed in: Last name First Name Email Address Doe John Code:
All help would be appreciated. I am new to Python and this seems to be quite a challenge for me. mmcclaf |
|
|
|
|
#2 |
|
Posts: n/a
|
mmcclaf wrote:
> Hi there, > > I have to make a small database using cPickle. I'm having troubles > trying to read in the information if it's more than one line. I'm > pretty sure it's in the line "for line in stuff:" Can anyone help me > out? Basically the end result is wanting it to look something like > what is down below when list is typed in: > > Last name First Name Email Address > Doe John > > > > Code:
> All help would be appreciated. I am new to Python and this seems to be > quite a challenge for me. Make sure you use modes "rb" and "wb" when you open the pickle files. If you are running on Windows this can make a difference. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Steve Holden |
|
|
|
#3 |
|
Posts: n/a
|
On Feb 6, 10:25*am, Steve Holden <st...@holdenweb.com> wrote:
> mmcclaf wrote: > > Hi there, > > > I have to make a small database using cPickle. I'm having troubles > > trying to read in the information if it's more than one line. I'm > > pretty sure it's in the line "for line in stuff:" Can anyone help me > > out? Basically the end result is wanting it to look something like > > what is down below when list is typed in: > > > Last name * * * * * * * * First Name * * * * * * * *Email Address > > Doe * * * * * * * * * * * * *John > > j...@doe.com > > > Code:
Code:
> > > All help would be appreciated. I am new to Python and this seems to be > > quite a challenge for me. > > Make sure you use modes "rb" and "wb" when you open the pickle files. If > you are running on Windows this can make a difference. > > regards > *Steve > -- > Steve Holden * * * *+1 571 484 6266 * +1 800 494 3119 > Holden Web LLC * * * * * * *http://www.holdenweb.com/ I've tried both rb and wb as well as r and w, there appears to be no difference in the running of the code. mmcclaf |
|
|
|
#4 |
|
Posts: n/a
|
mmcclaf wrote:
> On Feb 6, 10:25 am, Steve Holden <st...@holdenweb.com> wrote: >> mmcclaf wrote: >>> Hi there, >>> I have to make a small database using cPickle. I'm having troubles >>> trying to read in the information if it's more than one line. I'm >>> pretty sure it's in the line "for line in stuff:" Can anyone help me >>> out? Basically the end result is wanting it to look something like >>> what is down below when list is typed in: >>> Last name First Name Email Address >>> Doe John >>> j...@doe.com >>> Code:
>>> quite a challenge for me. >> Make sure you use modes "rb" and "wb" when you open the pickle files. If >> you are running on Windows this can make a difference. >> >> regards >> Steve >> -- >> Steve Holden +1 571 484 6266 +1 800 494 3119 >> Holden Web LLC http://www.holdenweb.com/ > > I've tried both rb and wb as well as r and w, there appears to be no > difference in the running of the code. > "cPickle.dump(work,output)" writes a string and "stuff=cPickle.load(input)" just reads that string, so "for line in stuff:" is iterating through the characters if the string. You need to use cPickle.load() to read each string (line). MRAB |
|
|
|
#5 |
|
Posts: n/a
|
On Feb 6, 3:09*pm, MRAB <goo...@mrabarnett.plus.com> wrote:
> mmcclaf wrote: > > On Feb 6, 10:25 am, Steve Holden <st...@holdenweb.com> wrote: > >> mmcclaf wrote: > >>> Hi there, > >>> I have to make a small database using cPickle. I'm having troubles > >>> trying to read in the information if it's more than one line. I'm > >>> pretty sure it's in the line "for line in stuff:" Can anyone help me > >>> out? Basically the end result is wanting it to look something like > >>> what is down below when list is typed in: > >>> Last name * * * * * * * * First Name * * * * * * * *Email Address > >>> Doe * * * * * * * * * * * * *John > >>> j...@doe.com > >>> Code:
> >>> quite a challenge for me. > >> Make sure you use modes "rb" and "wb" when you open the pickle files. If > >> you are running on Windows this can make a difference. > > >> regards > >> *Steve > >> -- > >> Steve Holden * * * *+1 571 484 6266 * +1 800 494 3119 > >> Holden Web LLC * * * * * * *http://www.holdenweb.com/ > > > I've tried both rb and wb as well as r and w, there appears to be no > > difference in the running of the code. > > "cPickle.dump(work,output)" writes a string and > "stuff=cPickle.load(input)" just reads that string, so "for line in > stuff:" is iterating through the characters if the string. You need to > use cPickle.load() to read each string (line). Ok, so I just modified that section to: Code:
And now it's printing it out ok, but then I get an EOFError at stuff=cPickle.load(onput) at line 45. mmcclaf |
|
|
|
#6 |
|
Posts: n/a
|
mmcclaf wrote:
> On Feb 6, 3:09 pm, MRAB <goo...@mrabarnett.plus.com> wrote: >> mmcclaf wrote: >>> On Feb 6, 10:25 am, Steve Holden <st...@holdenweb.com> wrote: >>>> mmcclaf wrote: >>>>> Hi there, >>>>> I have to make a small database using cPickle. I'm having troubles >>>>> trying to read in the information if it's more than one line. I'm >>>>> pretty sure it's in the line "for line in stuff:" Can anyone help me >>>>> out? Basically the end result is wanting it to look something like >>>>> what is down below when list is typed in: >>>>> Last name First Name Email Address >>>>> Doe John >>>>> j...@doe.com >>>>> Code:
>>>>> quite a challenge for me. >>>> Make sure you use modes "rb" and "wb" when you open the pickle files. If >>>> you are running on Windows this can make a difference. >>>> regards >>>> Steve >>>> -- >>>> Steve Holden +1 571 484 6266 +1 800 494 3119 >>>> Holden Web LLC http://www.holdenweb.com/ >>> I've tried both rb and wb as well as r and w, there appears to be no >>> difference in the running of the code. >> "cPickle.dump(work,output)" writes a string and >> "stuff=cPickle.load(input)" just reads that string, so "for line in >> stuff:" is iterating through the characters if the string. You need to >> use cPickle.load() to read each string (line). > > Ok, so I just modified that section to: > Code:
Code:
> > And now it's printing it out ok, but then I get an EOFError at > stuff=cPickle.load(onput) at line 45. > You can just keep reading until EOFError occurs, at which point you know you've reached the end of the file: input = open(filename, 'rb') try: while True: stuff = cPickle.load(input) print stuff except EOFError: pass input.close() MRAB |
|
|
|
#7 |
|
Posts: n/a
|
Thanks! All fixed!
mmcclaf |
|