Using cPickle

M

mmcclaf

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
(e-mail address removed)


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.
 
S

Steve Holden

mmcclaf said:
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
(e-mail address removed)


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
 
M

mmcclaf

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

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[/QUOTE]

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.
 
M

MRAB

mmcclaf said:
mmcclaf said:
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
(e-mail address removed)
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

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.
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).
 
M

mmcclaf

mmcclaf said:
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
(e-mail address removed)
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
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.

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.
 
M

MRAB

mmcclaf said:
mmcclaf said:
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
(e-mail address removed)
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.
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:[/QUOTE]
You're still iterating over the string.
[QUOTE]
#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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top