Hi everyone! I'm a newbie here.

I have a problem:
I need to create the address book in python that include name, phone number, email, address, etc...The information in the address book can able to delete or change.
I will give more information later!

Can you help me? I need to solve this problem quickly!

Thanks a lot!!

Dani AI

Generated

Nice thread. I agree with about starting simple, and with that moving to sqlite3 later will pay off. ’s pickle demo is fine for learning, but do not unpickle data you did not create yourself. If you want human‑readable files and easy import/export across languages (as noted), JSON is a good first storage format.

Here is a tiny JSON-backed core you can drop into a CLI and expand. It gives you load/save plus add/update/delete without classes, so you can focus on the menu and validation first.

# Python 3
import json, os, uuid

DB = "addressbook.json"

def load_db():
    try:
        with open(DB, "r") as f:
            return json.load(f)
    except FileNotFoundError:
        return {}

def save_db(db):
    tmp = DB + ".tmp"
    with open(tmp, "w") as f:
        json.dump(db, f, indent=2)
    os.replace(tmp, DB)  # atomic write

def add_contact(db, name, phone, email, address):
    cid = str(uuid.uuid4())
    db[cid] = {"name": name, "phone": phone, "email": email, "address": address}
    return cid

def update_contact(db, cid, **fields):
    if cid in db:
        db[cid].update({k: v for k, v in fields.items() if v is not None})

def delete_contact(db, cid):
    db.pop(cid, None)

Suggested next steps:

  • Build a simple loop: list, add, edit, delete, search-by-name prefix.
  • Validate input (e.g., basic email contains "@", phone digits only) and avoid bare except.
  • Keep a backup file before destructive operations.
  • When you outgrow JSON, switch storage only: keep the same functions, but implement them with sqlite3 (e.g., a contacts table with id TEXT PRIMARY KEY, name, phone, email UNIQUE, address). This keeps your UI untouched while gaining query speed and data integrity.

Recommended Answers

All 9 Replies

Start by making a function that allows you to read and write the data from/into text files.

If your data has be in text files, do what SgtMe suggested.
Alternatively, look into sqlite3 python module. It would allow you to create a real database and run sql statements against it to add, modify, and delete records.

That would be better overal, sergb, but I feel that GothLolita should start off using text-files. You can use this to have test data for testing your program, and add full databse support later.

help me in making address book!

Functions to add, remove and view entries to the address book, values stored in a pickled dictionary.

I'm eager to help, but I want to see some code effort from you.

Cheers and Happy coding

Use either a pickled dictionary as said by Beat_Slayer or if you want portability, use json module of python. (JavaScript Object Notation)

I'll get you a start point.

try:
    import cPickle as pickle
except:
    import pickle


class address_book():

    def __init__(self):
        try:
            self.__db = open('addressbook.db', 'rb')
            self.__entries = pickle.load(self.__db)
            self.__db.close()
        except:
            self.__db = open('addressbook.db', 'wb')
            self.__entries = {}
            self.__db.close()

    def __update(self):
        self.__db = open('addressbook.db', 'wb')
        pickle.dump(self.__entries, self.__db, -1)
        self.__db.close()

    def add_entry(self, (name, number, email)):
        self.__entries[name] = {}
        self.__entries[name]['Telephone'] = number
        self.__entries[name]['E-mail'] = email
        self.__update()

    def show_data(self):
        for k, v in self.__entries.iteritems():
            print 'Name:', k
            for subk, subv in v.iteritems():
                print '%s: %s' % (subk, subv)
            print
        

app = address_book()

userdata = ('Paul', '00316123456789', 'something@somewhere.com')

app.add_entry(userdata)

app.show_data()

Implement the rest,

Cheers and Happy coding

Why is he adding portability adding json???

Json is more dedicated to data interchange, pickle it's here used to store data to the file, not to send serialized data over network or to databases.

As the address book increases in datasize, I believe cPickle will make you feel the difference.

Cheers and Happy coding

Edit:

One reason to use cPickle???

import cPickle as pickle
import json
import time

def time_it(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        print '%s took %0.3f ms' % (func.func_name, (t2 - t1) * 1000.0)
        return res
    return wrapper

list1 = [1, 2, 3, 'string1', 'string2', 'string3',
         [23, 35, 46], {'somewhere': 1, 'elsewhere': 4}]

print list1

@time_it
def test_pickle():
    for run in range(100000):
        pickle.dumps(list1)

@time_it
def test_json():
    for run in range(100000):
        json.dumps(list1)

test_pickle()
test_json()

It's really fast.

Cheers and Happy coding

Maybe I used the wrong word "portability":$. Actually I wanted to say that if you want to create a project that is to be used by different coders following different languages, than you can always go for the json module. Just a piece of knowledge I had and I wanted to share. I didn't know about the speed problem. :-O
And if anyone wants to see the languages supported by json: json.org

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.