Using cPickle

Discussion in 'Python' started by mmcclaf, Feb 6, 2009.

  1. mmcclaf

    mmcclaf Guest

    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, Feb 6, 2009
    #1
    1. Advertising

  2. mmcclaf

    Steve Holden Guest

    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, Feb 6, 2009
    #2
    1. Advertising

  3. mmcclaf

    mmcclaf Guest

    On Feb 6, 10:25 am, Steve Holden <> 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
    > >

    >
    > >
    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[/color]
    >[color=green]
    > > #
    > > #!usr/bin/python
    > > # -*- coding: utf-8 -*-[/color]
    >[color=green]
    > > import sys
    > > import cPickle[/color]
    >[color=green]
    > > #
    > > format = '%s             %s                  %s'[/color]
    >[color=green]
    > > try:
    > >    filename = sys.argv[1]
    > >    input = open(filename, 'r')[/color]
    >[color=green]
    > > 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'[/color]
    >[color=green]
    > > #datas = cPickle.load(input)[/color]
    >[color=green]
    > > 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[/color]
    >[color=green]
    > >            input.close()[/color]
    >[color=green]
    > >    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)[/color]
    >[color=green]
    > >    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()[/color]
    >[color=green]
    > > 

    >
    > > 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, Feb 6, 2009
    #3
  4. mmcclaf

    MRAB Guest

    mmcclaf wrote:
    > On Feb 6, 10:25 am, Steve Holden <> 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
    >>>
    >>>
    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, Feb 6, 2009
    #4
  5. mmcclaf

    mmcclaf Guest

    On Feb 6, 3:09 pm, MRAB <> wrote:
    > mmcclaf wrote:
    > > On Feb 6, 10:25 am, Steve Holden <> 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
    > >>>
    > >>>
    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, Feb 6, 2009
    #5
  6. mmcclaf

    MRAB Guest

    mmcclaf wrote:
    > On Feb 6, 3:09 pm, MRAB <> wrote:
    >> mmcclaf wrote:
    >>> On Feb 6, 10:25 am, Steve Holden <> 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
    >>>>>
    >>>>>
    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:[/color]
    You're still iterating over the string.
    [color=blue]
    > 			#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, Feb 6, 2009
    #6
  7. mmcclaf

    mmcclaf Guest

    Thanks! All fixed!
     
    mmcclaf, Feb 26, 2009
    #7
    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. Drochom

    cPickle alternative?

    Drochom, Aug 15, 2003, in forum: Python
    Replies:
    13
    Views:
    885
    Michael Peuser
    Aug 17, 2003
  2. Carsten Gips

    Jython: jythonc and cPickle

    Carsten Gips, Sep 9, 2003, in forum: Python
    Replies:
    0
    Views:
    428
    Carsten Gips
    Sep 9, 2003
  3. Jesse Bloom

    problem using pickle / cPickle

    Jesse Bloom, Jan 2, 2004, in forum: Python
    Replies:
    1
    Views:
    388
    Vojin Jovanovic
    Jan 3, 2004
  4. Rune Strand

    Strange KeyError using cPickle

    Rune Strand, Jun 2, 2005, in forum: Python
    Replies:
    3
    Views:
    380
    Tim Peters
    Jun 2, 2005
  5. Replies:
    11
    Views:
    835
Loading...

Share This Page