python电话号码录入
时间: 2023-11-15 08:01:17 浏览: 43
Python电话号码录入可以使用字典来实现。字典中的键可以是联系人的姓名,值可以是联系人的电话号码等信息。在程序中,可以通过输入联系人的姓名来查找相关联系人的联系方式等信息,也可以在其中新增联系人,或修改、删除联系人信息。在输入联系人的电话号码时,可以使用判断输入是否为整数的方法来确保输入的是正确的电话号码格式。具体实现可以参考上述提供的引用资料。
相关问题
python设计通讯录并录入excel
要设计一个通讯录,你可以使用 Python 的字典(dictionary)来存储联系人的信息。每个联系人可以用一个字典来表示,其中包括姓名、电话号码、电子邮箱等信息。你可以将所有联系人的信息存储在一个列表(list)里。
以下是一个示例代码,用于添加、修改和删除联系人信息,并将所有联系人信息存储在一个 Excel 文件里。你需要使用 openpyxl 模块来操作 Excel 文件。
```python
import openpyxl
def add_contact(contacts):
name = input("请输入姓名:")
phone = input("请输入电话号码:")
email = input("请输入电子邮箱:")
contact = {"姓名": name, "电话号码": phone, "电子邮箱": email}
contacts.append(contact)
print("联系人添加成功!")
def modify_contact(contacts):
name = input("请输入要修改的联系人姓名:")
for contact in contacts:
if contact["姓名"] == name:
phone = input("请输入新的电话号码:")
email = input("请输入新的电子邮箱:")
contact["电话号码"] = phone
contact["电子邮箱"] = email
print("联系人信息修改成功!")
return
print("联系人不存在!")
def delete_contact(contacts):
name = input("请输入要删除的联系人姓名:")
for contact in contacts:
if contact["姓名"] == name:
contacts.remove(contact)
print("联系人删除成功!")
return
print("联系人不存在!")
def save_contacts_to_excel(contacts):
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "联系人列表"
sheet["A1"] = "姓名"
sheet["B1"] = "电话号码"
sheet["C1"] = "电子邮箱"
for i, contact in enumerate(contacts, start=2):
sheet.cell(row=i, column=1, value=contact["姓名"])
sheet.cell(row=i, column=2, value=contact["电话号码"])
sheet.cell(row=i, column=3, value=contact["电子邮箱"])
wb.save("contacts.xlsx")
def main():
contacts = []
while True:
print("=" * 30)
print("1. 添加联系人")
print("2. 修改联系人")
print("3. 删除联系人")
print("4. 保存联系人信息到 Excel 文件")
print("5. 退出程序")
choice = input("请输入选项:")
if choice == "1":
add_contact(contacts)
elif choice == "2":
modify_contact(contacts)
elif choice == "3":
delete_contact(contacts)
elif choice == "4":
save_contacts_to_excel(contacts)
elif choice == "5":
break
else:
print("无效选项!")
if __name__ == "__main__":
main()
```
这个程序可以在命令行界面中运行,根据提示输入相应的选项。当你选择将联系人信息保存到 Excel 文件时,程序会自动在程序运行的目录下创建一个名为 "contacts.xlsx" 的 Excel 文件,并将所有联系人信息存储在其中。
python设计通讯录系统并录入excel
可以使用Python中的xlwt和xlrd模块来读、写Excel文件,同时使用tkinter模块来设计通讯录系统的GUI界面。下面是一个示例代码,用于设计通讯录系统并录入Excel:
```
import tkinter as tk
import xlwt
import xlrd
class AddressBook:
def __init__(self, master):
self.master = master
master.title("通讯录系统")
# 创建GUI界面
self.label_name = tk.Label(master, text="姓名:")
self.label_name.grid(row=0, column=0)
self.entry_name = tk.Entry(master)
self.entry_name.grid(row=0, column=1)
self.label_phone = tk.Label(master, text="电话:")
self.label_phone.grid(row=1, column=0)
self.entry_phone = tk.Entry(master)
self.entry_phone.grid(row=1, column=1)
self.label_email = tk.Label(master, text="邮箱:")
self.label_email.grid(row=2, column=0)
self.entry_email = tk.Entry(master)
self.entry_email.grid(row=2, column=1)
self.button_add = tk.Button(master, text="添加", command=self.add_contact)
self.button_add.grid(row=3, column=0)
# 创建Excel文件和Sheet
self.workbook = xlwt.Workbook()
self.worksheet = self.workbook.add_sheet("通讯录")
self.row = 0
self.col = 0
# 写入表头
self.worksheet.write(self.row, self.col, "姓名")
self.col += 1
self.worksheet.write(self.row, self.col, "电话")
self.col += 1
self.worksheet.write(self.row, self.col, "邮箱")
self.row += 1
self.col = 0
def add_contact(self):
# 获取用户输入的信息
name = self.entry_name.get()
phone = self.entry_phone.get()
email = self.entry_email.get()
# 写入数据到Excel文件中
self.worksheet.write(self.row, self.col, name)
self.col += 1
self.worksheet.write(self.row, self.col, phone)
self.col += 1
self.worksheet.write(self.row, self.col, email)
self.row += 1
self.col = 0
# 清空文本框
self.entry_name.delete(0, tk.END)
self.entry_phone.delete(0, tk.END)
self.entry_email.delete(0, tk.END)
# 保存到文件
self.workbook.save("通讯录.xls")
root = tk.Tk()
address_book = AddressBook(root)
root.mainloop()
```
在上面的示例代码中,我们首先使用tkinter模块创建了一个GUI界面,包含三个文本框和一个添加按钮。当用户点击添加按钮时,我们从文本框中获取用户输入的信息,并将其写入Excel文件中。同时,我们也在GUI界面中显示了已经录入的数据。最后,我们将Excel文件保存到了本地磁盘上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)