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()
> 
> [/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/

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to