Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Using cPickle

 
Thread Tools Search this Thread
Old 02-06-2009, 02:30 PM   #1
Default Using cPickle


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:
# @author: Ocdt Murray McClafferty 24656 # This will manage a small database using the cPickle module. # It must maintain a list of last names, first names and email addresses, and must let a user interact with the program # #!usr/bin/python # -*- coding: utf-8 -*- import sys import cPickle # format = '%s %s %s' try: filename = sys.argv[1] input = open(filename, 'r') except IOError: print 'File is not available, will create a new file now' lastName='Last Name' firstName='First Name' email= 'Email' #input.close() output=open (filename, 'w') total = format%(lastName, firstName, email) cPickle.dump(total,output) #cPickle.dump(firstName,output) #cPickle.dump(email,output) output.close() except EOFError: print 'File is empty' #datas = cPickle.load(input) while True: command=sys.stdin.readline()[:-1] if command=='list': #lists the data in the file input = open(filename, 'r') stuff=cPickle.load(input) for line in stuff: #firstName=cPickle.load(input) #email=cPickle.load(input) #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr (email).rjust(20) stuff=cPickle.load(input) print stuff print line input.close() if command=='exit' or command=='quit' : #NEVER forget the exit!!! print 'Save changes? y for Yes, n for No' commandSave=sys.stdin.readline()[:-1] if commandSave =='y': #if the user wants to save output=open(filename, 'w') cPickle.dump(work,output) output.close() sys.exit(0) if commandSave =='n': #no save input.close() sys.exit(0) if command=='add': #adds an entity to the file print 'Last name?' lastName=sys.stdin.readline()[:-1] print 'First name?' firstName=sys.stdin.readline()[:-1] print 'Email address?' email=sys.stdin.readline()[:-1] work = format%(lastName, firstName, email) #output=open(filename, 'w') #data=cPickle.load(output) #data.append(work) #output.close() output=open(filename, 'a') cPickle.dump(work,output) output.close()

All help would be appreciated. I am new to Python and this seems to be
quite a challenge for me.


mmcclaf
  Reply With Quote
Old 02-06-2009, 03:25 PM   #2
Steve Holden
 
Posts: n/a
Default Re: Using cPickle
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:
> # @author: Ocdt Murray McClafferty 24656 > # This will manage a small database using the cPickle module. > # It must maintain a list of last names, first names and email > addresses, and must let a user interact with the program > > # > #!usr/bin/python > # -*- coding: utf-8 -*- > > import sys > import cPickle > > # > format = '%s %s %s' > > try: > filename = sys.argv[1] > input = open(filename, 'r') > > except IOError: > print 'File is not available, will create a new file now' > lastName='Last Name' > firstName='First Name' > email= 'Email' > #input.close() > output=open (filename, 'w') > total = format%(lastName, firstName, email) > cPickle.dump(total,output) > #cPickle.dump(firstName,output) > #cPickle.dump(email,output) > output.close() > except EOFError: > print 'File is empty' > > #datas = cPickle.load(input) > > while True: > command=sys.stdin.readline()[:-1] > if command=='list': #lists the data in the file > input = open(filename, 'r') > stuff=cPickle.load(input) > for line in stuff: > #firstName=cPickle.load(input) > #email=cPickle.load(input) > #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr > (email).rjust(20) > stuff=cPickle.load(input) > print stuff > print line > > input.close() > > if command=='exit' or command=='quit' : #NEVER forget the exit!!! > print 'Save changes? y for Yes, n for No' > commandSave=sys.stdin.readline()[:-1] > if commandSave =='y': #if the user wants to save > output=open(filename, 'w') > cPickle.dump(work,output) > output.close() > sys.exit(0) > if commandSave =='n': #no save > input.close() > sys.exit(0) > > if command=='add': #adds an entity to the file > print 'Last name?' > lastName=sys.stdin.readline()[:-1] > print 'First name?' > firstName=sys.stdin.readline()[:-1] > print 'Email address?' > email=sys.stdin.readline()[:-1] > work = format%(lastName, firstName, email) > #output=open(filename, 'w') > #data=cPickle.load(output) > #data.append(work) > #output.close() > output=open(filename, 'a') > cPickle.dump(work,output) > output.close() > >
>
> 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
  Reply With Quote
Old 02-06-2009, 07:05 PM   #3
mmcclaf
 
Posts: n/a
Default Re: Using cPickle
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:
> > # @author: Ocdt Murray McClafferty 24656 > > # This will manage a small database using the cPickle module. > > # It must maintain a list of last names, first names and email > > addresses, and must let a user interact with the program
Code:
> > > # > > #!usr/bin/python > > # -*- coding: utf-8 -*- > > > import sys > > import cPickle > > > # > > format = '%s * * * * * * %s * * * * * * * * *%s' > > > try: > > * *filename = sys.argv[1] > > * *input = open(filename, 'r') > > > except IOError: > > * *print 'File is not available, will create a new file now' > > * *lastName='Last Name' > > * *firstName='First Name' > > * *email= 'Email' > > * *#input.close() > > * *output=open (filename, 'w') > > * *total = format%(lastName, firstName, email) > > * *cPickle.dump(total,output) > > * *#cPickle.dump(firstName,output) > > * *#cPickle.dump(email,output) > > * *output.close() > > except EOFError: > > * *print 'File is empty' > > > #datas = cPickle.load(input) > > > while True: > > * *command=sys.stdin.readline()[:-1] > > * *if command=='list': #lists the data in the file > > * * * * * *input = open(filename, 'r') > > * * * * * *stuff=cPickle.load(input) > > * * * * * *for line in stuff: > > * * * * * * * * * *#firstName=cPickle.load(input) > > * * * * * * * * * *#email=cPickle.load(input) > > * * * * * * * * * *#print repr (lastName).rjust(10), repr(firstName).rjust(20), repr > > (email).rjust(20) > > * * * * * * * * * *stuff=cPickle.load(input) > > * * * * * * * * * *print stuff > > * * * * * * * * * *print line > > > * * * * * *input.close() > > > * *if command=='exit' or command=='quit' : #NEVER forget the exit!!! > > * * * * * *print 'Save changes? y for Yes, n for No' > > * * * * * *commandSave=sys.stdin.readline()[:-1] > > * * * * * *if commandSave =='y': #if the user wants to save > > * * * * * * * * * *output=open(filename, 'w') > > * * * * * * * * * *cPickle.dump(work,output) > > * * * * * * * * * *output.close() > > * * * * * * * * * *sys.exit(0) > > * * * * * *if commandSave =='n': #no save > > * * * * * * * * * *input.close() > > * * * * * * * * * *sys.exit(0) > > > * *if command=='add': #adds an entity to the file > > * * * * * *print 'Last name?' > > * * * * * *lastName=sys.stdin.readline()[:-1] > > * * * * * *print 'First name?' > > * * * * * *firstName=sys.stdin.readline()[:-1] > > * * * * * *print 'Email address?' > > * * * * * *email=sys.stdin.readline()[:-1] > > * * * * * *work = format%(lastName, firstName, email) > > * * * * * *#output=open(filename, 'w') > > * * * * * *#data=cPickle.load(output) > > * * * * * *#data.append(work) > > * * * * * *#output.close() > > * * * * * *output=open(filename, 'a') > > * * * * * *cPickle.dump(work,output) > > * * * * * *output.close() > > >

>
> > 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
  Reply With Quote
Old 02-06-2009, 08:09 PM   #4
MRAB
 
Posts: n/a
Default Re: Using cPickle
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:
>>> # @author: Ocdt Murray McClafferty 24656 >>> # This will manage a small database using the cPickle module. >>> # It must maintain a list of last names, first names and email >>> addresses, and must let a user interact with the program >>> # >>> #!usr/bin/python >>> # -*- coding: utf-8 -*- >>> import sys >>> import cPickle >>> # >>> format = '%s %s %s' >>> try: >>> filename = sys.argv[1] >>> input = open(filename, 'r') >>> except IOError: >>> print 'File is not available, will create a new file now' >>> lastName='Last Name' >>> firstName='First Name' >>> email= 'Email' >>> #input.close() >>> output=open (filename, 'w') >>> total = format%(lastName, firstName, email) >>> cPickle.dump(total,output) >>> #cPickle.dump(firstName,output) >>> #cPickle.dump(email,output) >>> output.close() >>> except EOFError: >>> print 'File is empty' >>> #datas = cPickle.load(input) >>> while True: >>> command=sys.stdin.readline()[:-1] >>> if command=='list': #lists the data in the file >>> input = open(filename, 'r') >>> stuff=cPickle.load(input) >>> for line in stuff: >>> #firstName=cPickle.load(input) >>> #email=cPickle.load(input) >>> #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr >>> (email).rjust(20) >>> stuff=cPickle.load(input) >>> print stuff >>> print line >>> input.close() >>> if command=='exit' or command=='quit' : #NEVER forget the exit!!! >>> print 'Save changes? y for Yes, n for No' >>> commandSave=sys.stdin.readline()[:-1] >>> if commandSave =='y': #if the user wants to save >>> output=open(filename, 'w') >>> cPickle.dump(work,output) >>> output.close() >>> sys.exit(0) >>> if commandSave =='n': #no save >>> input.close() >>> sys.exit(0) >>> if command=='add': #adds an entity to the file >>> print 'Last name?' >>> lastName=sys.stdin.readline()[:-1] >>> print 'First name?' >>> firstName=sys.stdin.readline()[:-1] >>> print 'Email address?' >>> email=sys.stdin.readline()[:-1] >>> work = format%(lastName, firstName, email) >>> #output=open(filename, 'w') >>> #data=cPickle.load(output) >>> #data.append(work) >>> #output.close() >>> output=open(filename, 'a') >>> cPickle.dump(work,output) >>> output.close() >>>
>>> 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.
>

"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
  Reply With Quote
Old 02-06-2009, 09:29 PM   #5
mmcclaf
 
Posts: n/a
Default Re: Using cPickle
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:
> >>> # @author: Ocdt Murray McClafferty 24656 > >>> # This will manage a small database using the cPickle module. > >>> # It must maintain a list of last names, first names and email > >>> addresses, and must let a user interact with the program > >>> # > >>> #!usr/bin/python > >>> # -*- coding: utf-8 -*- > >>> import sys > >>> import cPickle > >>> # > >>> format = '%s * * * * * * %s * * * * * * * * *%s' > >>> try: > >>> * *filename = sys.argv[1] > >>> * *input = open(filename, 'r') > >>> except IOError: > >>> * *print 'File is not available, will create a new file now' > >>> * *lastName='Last Name' > >>> * *firstName='First Name' > >>> * *email= 'Email' > >>> * *#input.close() > >>> * *output=open (filename, 'w') > >>> * *total = format%(lastName, firstName, email) > >>> * *cPickle.dump(total,output) > >>> * *#cPickle.dump(firstName,output) > >>> * *#cPickle.dump(email,output) > >>> * *output.close() > >>> except EOFError: > >>> * *print 'File is empty' > >>> #datas = cPickle.load(input) > >>> while True: > >>> * *command=sys.stdin.readline()[:-1] > >>> * *if command=='list': #lists the data in the file > >>> * * * * * *input = open(filename, 'r') > >>> * * * * * *stuff=cPickle.load(input) > >>> * * * * * *for line in stuff: > >>> * * * * * * * * * *#firstName=cPickle.load(input) > >>> * * * * * * * * * *#email=cPickle.load(input) > >>> * * * * * * * * * *#print repr (lastName).rjust(10), repr(firstName).rjust(20), repr > >>> (email).rjust(20) > >>> * * * * * * * * * *stuff=cPickle.load(input) > >>> * * * * * * * * * *print stuff > >>> * * * * * * * * * *print line > >>> * * * * * *input.close() > >>> * *if command=='exit' or command=='quit' : #NEVER forget the exit!!! > >>> * * * * * *print 'Save changes? y for Yes, n for No' > >>> * * * * * *commandSave=sys.stdin.readline()[:-1] > >>> * * * * * *if commandSave =='y': #if the user wants to save > >>> * * * * * * * * * *output=open(filename, 'w') > >>> * * * * * * * * * *cPickle.dump(work,output) > >>> * * * * * * * * * *output.close() > >>> * * * * * * * * * *sys.exit(0) > >>> * * * * * *if commandSave =='n': #no save > >>> * * * * * * * * * *input.close() > >>> * * * * * * * * * *sys.exit(0) > >>> * *if command=='add': #adds an entity to the file > >>> * * * * * *print 'Last name?' > >>> * * * * * *lastName=sys.stdin.readline()[:-1] > >>> * * * * * *print 'First name?' > >>> * * * * * *firstName=sys.stdin.readline()[:-1] > >>> * * * * * *print 'Email address?' > >>> * * * * * *email=sys.stdin.readline()[:-1] > >>> * * * * * *work = format%(lastName, firstName, email) > >>> * * * * * *#output=open(filename, 'w') > >>> * * * * * *#data=cPickle.load(output) > >>> * * * * * *#data.append(work) > >>> * * * * * *#output.close() > >>> * * * * * *output=open(filename, 'a') > >>> * * * * * *cPickle.dump(work,output) > >>> * * * * * *output.close() > >>>
> >>> 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.

>
> "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:
if command=='list': #lists the data in the file input = open(filename, 'r') stuff=cPickle.load(input) for line in stuff: #firstName=cPickle.load(input) #email=cPickle.load(input) #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr (email).rjust(20) stuff=cPickle.load(input) print stuff input.close()

And now it's printing it out ok, but then I get an EOFError at
stuff=cPickle.load(onput) at line 45.


mmcclaf
  Reply With Quote
Old 02-06-2009, 10:04 PM   #6
MRAB
 
Posts: n/a
Default Re: Using cPickle
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:
>>>>> # @author: Ocdt Murray McClafferty 24656 >>>>> # This will manage a small database using the cPickle module. >>>>> # It must maintain a list of last names, first names and email >>>>> addresses, and must let a user interact with the program >>>>> # >>>>> #!usr/bin/python >>>>> # -*- coding: utf-8 -*- >>>>> import sys >>>>> import cPickle >>>>> # >>>>> format = '%s %s %s' >>>>> try: >>>>> filename = sys.argv[1] >>>>> input = open(filename, 'r') >>>>> except IOError: >>>>> print 'File is not available, will create a new file now' >>>>> lastName='Last Name' >>>>> firstName='First Name' >>>>> email= 'Email' >>>>> #input.close() >>>>> output=open (filename, 'w') >>>>> total = format%(lastName, firstName, email) >>>>> cPickle.dump(total,output) >>>>> #cPickle.dump(firstName,output) >>>>> #cPickle.dump(email,output) >>>>> output.close() >>>>> except EOFError: >>>>> print 'File is empty' >>>>> #datas = cPickle.load(input) >>>>> while True: >>>>> command=sys.stdin.readline()[:-1] >>>>> if command=='list': #lists the data in the file >>>>> input = open(filename, 'r') >>>>> stuff=cPickle.load(input) >>>>> for line in stuff: >>>>> #firstName=cPickle.load(input) >>>>> #email=cPickle.load(input) >>>>> #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr >>>>> (email).rjust(20) >>>>> stuff=cPickle.load(input) >>>>> print stuff >>>>> print line >>>>> input.close() >>>>> if command=='exit' or command=='quit' : #NEVER forget the exit!!! >>>>> print 'Save changes? y for Yes, n for No' >>>>> commandSave=sys.stdin.readline()[:-1] >>>>> if commandSave =='y': #if the user wants to save >>>>> output=open(filename, 'w') >>>>> cPickle.dump(work,output) >>>>> output.close() >>>>> sys.exit(0) >>>>> if commandSave =='n': #no save >>>>> input.close() >>>>> sys.exit(0) >>>>> if command=='add': #adds an entity to the file >>>>> print 'Last name?' >>>>> lastName=sys.stdin.readline()[:-1] >>>>> print 'First name?' >>>>> firstName=sys.stdin.readline()[:-1] >>>>> print 'Email address?' >>>>> email=sys.stdin.readline()[:-1] >>>>> work = format%(lastName, firstName, email) >>>>> #output=open(filename, 'w') >>>>> #data=cPickle.load(output) >>>>> #data.append(work) >>>>> #output.close() >>>>> output=open(filename, 'a') >>>>> cPickle.dump(work,output) >>>>> output.close() >>>>>
>>>>> 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.

>> "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:
> if command=='list': #lists the data in the file > input = open(filename, 'r') > stuff=cPickle.load(input) > for line in stuff:
Code:
You're still iterating over the string. > #firstName=cPickle.load(input) > #email=cPickle.load(input) > #print repr (lastName).rjust(10), repr(firstName).rjust(20), repr > (email).rjust(20) > stuff=cPickle.load(input) > print stuff > > > input.close() >

>
> 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
  Reply With Quote
Old 02-26-2009, 03:31 AM   #7
mmcclaf
 
Posts: n/a
Default Re: Using cPickle
Thanks! All fixed!


mmcclaf
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46