python: 实现通讯录录入与查找的小脚本

python: 实现通讯录录入与查找的小脚本

标签:python 通讯录 脚本

by 小威威

今天我用python写了两个小脚本,有了它们,我就可以实现通讯录的录入与查找,挺方便的。

直接上代码:
脚本1:实现通讯录的录入。

#!/usr/bin/python3
# Filename: address_list.py

# Call pickle module to store the message in the file
import pickle

# Define a class name classmates
class classmates:
    def __init__(self, name, age, phonenumber, email_address):
        self.name = name
        self.age = age
        self.phonenumber = phonenumber
        self.email_address = email_address

# Through the while, we can put more group of data in it
while True:
    name = input('Please enter your name:')
    age = input('Please enter your age:')
    phonenumber = input('Please enter your phonenumber:')
    email_address = input('Please enter your email_address:')
    lab = classmates(name, age, phonenumber, email_address)
    info = {
        'name': '%s' % lab.name, 
        'age': '%s' % lab.age,
        'phonenumber': '%s' % lab.phonenumber,
        'email_address': '%s' % lab.email_address
        }
    filename = '%s.txt' % name
    f = open(filename, 'wb')
    pickle.dump(info, f)
    f.close()
    # This is the condition of stopping the loop
    choice = input('Do you want to continue,enter Y or N:')
    if choice == 'N':
        break

脚本二:实现通讯录的查找

#!/usr/bin/python3
# Filename: search_info.py

# Call the module of pickle to load the message import pickle # Through the message, we can achieve searching the info for more times while True: name = input('Please enter the person you want:') filename = '%s.txt' % name f = open(filename, 'rb') info = pickle.load(f) print ('Please enter the message you want,', end = '') message = input('age, phonenumber or email_address:') print(info[message]) choice = input('Do you want to print all its information,enter Y or N:') if choice == 'Y': for key, content in info.items(): print('%s : %s' %(key, content)) # This is the condition to stop the loop choice2 = input('Do you want to search others,enter Y or N:') if choice2 == 'N': break;

f.close()

以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨。

你可能感兴趣的:(python,脚本,通讯录)