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 NameEmail Address Doe John [email protected] [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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using cPickle
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 > > [email protected] > > > [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/ 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using cPickle
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 > >>> [email protected] > >>> [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/ > > > 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() [/code] And now it's printing it out ok, but then I get an EOFError at stuff=cPickle.load(onput) at line 45. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using cPickle
Thanks! All fixed! -- http://mail.python.org/mailman/listinfo/python-list
Queries
I have to make some queries for 4 tables I have. The following relations are: Classes(class, type, country, numGuns, bore, displacement) Ships (name, class, launched) Battles (name, date) Outcomes (ship, battle, result) The three queries I'm stuck on are the following: 1. Find the classes that have only one ship as a member of that class (not all ships are listed in the Ship table) 2. Find the countries that had both battleships and battlecruisers (those fall under type in Classes) 3. Find those ships that "lived to fight another day"; they were damaged in one battle, but later fought in another. The best way for me to understand would be relational algebra for each of the statements. Any help in this would be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
On Feb 26, 7:51 am, Steve Holden wrote: > Murray wrote: > > [top-posting corrected] > > > > > -Original Message- > > From: Gary Herron [mailto:[email protected]] > > Sent: Thursday, February 26, 2009 1:46 AM > > To: mmcclaf; [email protected] > > Subject: Re: Queries > > > mmcclaf wrote: > >> I have to make some queries for 4 tables I have. The following > >> relations are: > > >> Classes(class, type, country, numGuns, bore, displacement) > >> Ships (name, class, launched) > >> Battles (name, date) > >> Outcomes (ship, battle, result) > > >> The three queries I'm stuck on are the following: > > >> 1. Find the classes that have only one ship as a member of that class > >> (not all ships are listed in the Ship table) > > Investigate a GROUP BY solution that selects groups having a count of 1. > > >> 2. Find the countries that had both battleships and battlecruisers > >> (those fall under type in Classes) > > Look at EXISTS for one possible solutions. > > >> 3. Find those ships that "lived to fight another day"; they were > >> damaged in one battle, but later fought in another. > > >From your model description I don't even see where the position and > > attitude of each ship is stored, so I don't think I can give you any > help at all with this one. > > >> The best way for me to understand would be relational algebra for each > >> of the statements. > > > Sounds like a homework assignment. Good luck with it. > > > It is, however I was able to get the first 8 done, I am struggling with > > these 3 particular ones. I have to make an SQL file based off of it, > so this > > seems to be a blockage in my works. > > Good luck with the homework. Remember to acknowledge the help you've had > from this list (particularly your earlier issues: here you just have hints). > > regards > Steve > -- > Steve Holden +1 571 484 6266 +1 800 494 3119 > Holden Web LLC http://www.holdenweb.com/ Yeppers always do. And thanks! As for the number 3 one... to know if they were damaged would appear in "Result" of the Outcomes table -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
Another problem with this assignment... I have to list all the ships mentioned in the database. All the ships may not appear in the Ships relations... I said the following in algebraic expression SELECT name(Ships UNION (SELECT ship (Outcome UNION(SELECT class (Classes))) Would that work? -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
This came in the Python groups, and I put one up in the database group, since I will later have to use Python to access the SQL file, so therefore tackling one thing at a time. Also, there were no answers that were coming up in the .database group. On top of that: >Really? You can have ships in the Outcomes relation that DO NOT >APPEAR in the Ships relation... Goodbye referential integrity. > >Besides which, if they don't appear in the Ships relation, then >there is NO INFORMATION to tell you what ship CLASS it is a member of -- >do you group them all as "UNKNOWN" class, or do you declare each to be >the only ship of its class (and thereby use the ship name as the class >name)? This meant that some ships weren't listed in the Ship table provided but did show up in the Outcome. Therefore, they are not directly referenced one to the other. > Another gmail user found only by back referencing responses... What is meant by this statement? So in short of the responses given, I need to study further: GROUP BY, HAVING, AS, COUNT, and subselect queries, right? The relational algebra, I am able to translate it into SQL, or most of it, meaning turning it into the queries of SQL. This is not an advanced database course, but more a basic intro to database systems. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
So basically you are complaining that groups don't pick up post by GMail users? As for the SQL thing, you still don't see how I am linking this to SQL? I have to make in your terms: a textual representation of the statements I'd make in SQL. The statement I made with the bold SELECT, etc, replace those with the relational algebra symbols, hence needing to make a relational algebra expression. >> So in short of the responses given, I need to study further: GROUP BY, >> HAVING, AS, COUNT, and subselect queries, right? >Since it's homework, we (this group) won't be giving direct SQL > statements... I never said I wanted direct SQL statements, moreso guidance as to what direction I should be heading for each of the queries. I just wanted to make sure that there might not be other things I might want to study as well that might be required for the queries. Try not to interpret what isn't there... I'm looking for help, not direct answers. -- http://mail.python.org/mailman/listinfo/python-list
