| | |
Adress book
![]() |
I think the easiest way to explain it is to say its like a list where you would use:
and in yours it would be something like:
please bare in mind that what ive added i havent tested so take it with a pinch of salt
python Syntax (Toggle Plain Text)
mylist = [] mylist.append('hello') print mylist #outputs ['hello']
python Syntax (Toggle Plain Text)
people = open('people_data.txt', 'a') class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) name_dic = {} # first set of data for testing name = 'frank millato' home = '123-456-7890' mobile = '456-789-0123' email = 'frank23@gmail.com' name_dic[name] = Data(home, mobile, email) # second set of data for testing name = 'bob dork' home = '234-567-8901' mobile = '567-690-1234' email = 'bob.dork@hotmail.com' name_dic[name] = Data(home, mobile, email) def change_att(): #c_search = raw_input("Enter name to alter: ") c_search = 'bob dork' # for testing only!!! if name_dic.has_key(c_search): #c_name = raw_input("What element do you want to change: ") c_name = 'email' # for tesing only!!! if c_name == 'email': #n_e = raw_input("Enter new email adress: ") n_e = 'bob.dork@yahoo.com' # for testing only!!! # make the change k = name_dic[c_search] name_dic[c_search] = Data(k.home, k.mobile, n_e) # get the info for all names added before change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) print('-'*40) change_att() # get the info for all names added after change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) #and then after sowing it you can append it: # get the info for all names added after change for key in name_dic: if key not in people.read(): people.write("Name = %s" % key) people.write(name_dic[key].get_info())
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
i just tested it and it had a couple of my errors so here is a fix:
python Syntax (Toggle Plain Text)
people = open('people_data.txt', 'a') people2 = open('people_data.txt', 'r') class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) name_dic = {} # first set of data for testing name = 'frank millato' home = '123-456-7890' mobile = '456-789-0123' email = 'frank23@gmail.com' name_dic[name] = Data(home, mobile, email) # second set of data for testing name = 'bob dork' home = '234-567-8901' mobile = '567-690-1234' email = 'bob.dork@hotmail.com' name_dic[name] = Data(home, mobile, email) def change_att(): #c_search = raw_input("Enter name to alter: ") c_search = 'bob dork' # for testing only!!! if name_dic.has_key(c_search): #c_name = raw_input("What element do you want to change: ") c_name = 'email' # for tesing only!!! if c_name == 'email': #n_e = raw_input("Enter new email adress: ") n_e = 'bob.dork@yahoo.com' # for testing only!!! # make the change k = name_dic[c_search] name_dic[c_search] = Data(k.home, k.mobile, n_e) # get the info for all names added before change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) print('-'*40) change_att() # get the info for all names added after change for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) #and then after sowing it you can append it: # get the info for all names added after change for key in name_dic: if key not in people2: people.write("Name = %s" % key) people.write(name_dic[key].get_info())
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
I did that in there but basically all it is that does it are 2 snippets:
python Syntax (Toggle Plain Text)
people = open('people_data.txt', 'a') people2 = open('people_data.txt', 'r') #which opens 2 versions of the file, 1 to append and 1 to read to make sure the information is all unique #and for key in name_dic: if key not in people2: people.write("Name = %s" % key) people.write(name_dic[key].get_info()) #that checks for previous entries that are the same and then writes entries that aren't #I cant really explain it, I'm going to have to rely on someone else to make it simple as the list example is my best attempt
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
I took the liberty to use sneekula's test setup ...
python Syntax (Toggle Plain Text)
# a test setup for pickle dump and load # note that pickle does not save the class itself # if the dumped data file is used in another program, # you need to code a matching class in that program import pickle class Data(object): def __init__(self, home, mobile, email): self.home = home self.mobile = mobile self.email = email self.get_info() def get_info(self): sf = \ """Home Phone: %s Mobile Phone: %s Email: %s """ return sf % (self.home, self.mobile, self.email) name_dic = {} # first set of data for testing name = 'frank millato' home = '123-456-7890' mobile = '456-789-0123' email = 'frank23@gmail.com' name_dic[name] = Data(home, mobile, email) # second set of data for testing name = 'bob dork' home = '234-567-8901' mobile = '567-690-1234' email = 'bob.dork@hotmail.com' name_dic[name] = Data(home, mobile, email) # get the info for all names before dump print( "Original data:" ) for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) print('-'*40) fname = "PhoneDict.dat" # pickle dump the dictionary fout = open(fname, "w") pickle.dump(name_dic, fout) fout.close() # delete the name_dic object del name_dic # pickle load the dictionary fin = open(fname, "r") name_dic = pickle.load(fin) fin.close() # get the info for all names after load print( "Data dumped and reloaded with module pickle:" ) for key in name_dic: print( "Name = %s" % key ) print( name_dic[key].get_info() ) """ my output --> Original data: Name = frank millato Home Phone: 123-456-7890 Mobile Phone: 456-789-0123 Email: frank23@gmail.com Name = bob dork Home Phone: 234-567-8901 Mobile Phone: 567-690-1234 Email: bob.dork@hotmail.com ---------------------------------------- Data dumped and reloaded with module pickle: Name = frank millato Home Phone: 123-456-7890 Mobile Phone: 456-789-0123 Email: frank23@gmail.com Name = bob dork Home Phone: 234-567-8901 Mobile Phone: 567-690-1234 Email: bob.dork@hotmail.com """
May 'the Google' be with you!
If you save the data as a text file like in your case, then you have to rebuild the entire dictionary each time when you load the saved data. As the size of the data grows, this gets to be time consuming. Also, if you edit the data and simply append to a text file, then the old data would still be there.
May 'the Google' be with you!
![]() |
Similar Threads
- ideas for project.. (C)
- Virtual Address Book Help (Python)
- address book won't open (OS X)
- writitng data from php to mysql (PHP)
- Beginner.........Books (MySQL)
- IE6 has been constantly hijacked by .... (Viruses, Spyware and other Nasties)
- Motorola T720 synching phone book (Cellphones, PDAs and Handheld Devices)
- Book Reviews (Geeks' Lounge)
- Error in Wrox Book (Perl)
Other Threads in the Python Forum
- Previous Thread: wxPython Error
- Next Thread: Help updating images and texts
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






