我想通过PYTHON查找displayName数据
时间: 2024-01-11 13:02:28 浏览: 109
可以使用 Python 中的 json 模块来解析 JSON 数据,然后使用字典的方式来访问 JSON 中的数据。具体代码如下:
```python
import json
# 假设 JSON 数据保存在 data.json 文件中
with open('data.json', 'r') as f:
data = json.load(f)
# 获取 displayName 数据
display_name = data['displayName']
print(display_name)
```
其中,`data['displayName']` 就是获取 JSON 数据中 displayName 的值。
相关问题
python通讯录程序,包含基本的查找、删除、修改、添加功能,并能实现数据的保存。200行代码以上
好的,下面是一个简单的Python通讯录程序,包含查找、删除、修改、添加功能,并能实现数据的保存。这个程序使用JSON文件来保存和加载数据。
```python
import json
import os
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def to_dict(self):
return {
'name': self.name,
'phone': self.phone,
'email': self.email
}
def load_contacts(file_path):
if not os.path.exists(file_path):
return []
with open(file_path, 'r') as file:
contacts_data = json.load(file)
return [Contact(**data) for data in contacts_data]
def save_contacts(file_path, contacts):
with open(file_path, 'w') as file:
contacts_data = [contact.to_dict() for contact in contacts]
json.dump(contacts_data, file, indent=4)
def find_contact(contacts, name):
return next((contact for contact in contacts if contact.name == name), None)
def add_contact(contacts, contact):
if find_contact(contacts, contact.name):
print(f"Contact {contact.name} already exists.")
else:
contacts.append(contact)
print(f"Contact {contact.name} added successfully.")
def delete_contact(contacts, name):
contact = find_contact(contacts, name)
if contact:
contacts.remove(contact)
print(f"Contact {name} deleted successfully.")
else:
print(f"Contact {name} not found.")
def modify_contact(contacts, name, new_phone, new_email):
contact = find_contact(contacts, name)
if contact:
contact.phone = new_phone
contact.email = new_email
print(f"Contact {name} modified successfully.")
else:
print(f"Contact {name} not found.")
def display_contacts(contacts):
if not contacts:
print("No contacts to display.")
return
for contact in contacts:
print(f"Name: {contact.name}, Phone: {contact.phone}, Email: {contact.email}")
def main():
file_path = 'contacts.json'
contacts = load_contacts(file_path)
while True:
print("\n1. Display Contacts")
print("2. Add Contact")
print("3. Delete Contact")
print("4. Modify Contact")
print("5. Find Contact")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == '1':
display_contacts(contacts)
elif choice == '2':
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email: ")
add_contact(contacts, Contact(name, phone, email))
save_contacts(file_path, contacts)
elif choice == '3':
name = input("Enter name: ")
delete_contact(contacts, name)
save_contacts(file_path, contacts)
elif choice == '4':
name = input("Enter name: ")
new_phone = input("Enter new phone: ")
new_email = input("Enter new email: ")
modify_contact(contacts, name, new_phone, new_email)
save_contacts(file_path, contacts)
elif choice == '5':
name = input("Enter name: ")
contact = find_contact(contacts, name)
if contact:
print(f"Name: {contact.name}, Phone: {contact.phone}, Email: {contact.email}")
else:
print(f"Contact {name} not found.")
elif choice == '6':
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
```
这个程序实现了基本的通讯录功能,包括查找、删除、修改、添加功能,并能实现数据的保存。数据保存在一个名为`contacts.json`的文件中。
用python数据结构顺序表编辑通讯录
好的,下面是一个简单的用Python实现的通讯录管理程序,使用顺序表作为数据结构:
```python
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def remove_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
break
def find_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
def edit_contact(self, name, phone, email):
for contact in self.contacts:
if contact.name == name:
contact.phone = phone
contact.email = email
break
def display_contacts(self):
for contact in self.contacts:
print("Name:", contact.name)
print("Phone:", contact.phone)
print("Email:", contact.email)
print()
def main():
address_book = AddressBook()
while True:
print("1. Add contact")
print("2. Remove contact")
print("3. Find contact")
print("4. Edit contact")
print("5. Display contacts")
print("6. Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email: ")
contact = Contact(name, phone, email)
address_book.add_contact(contact)
print("Contact added.")
elif choice == 2:
name = input("Enter name: ")
address_book.remove_contact(name)
print("Contact removed.")
elif choice == 3:
name = input("Enter name: ")
contact = address_book.find_contact(name)
if contact:
print("Name:", contact.name)
print("Phone:", contact.phone)
print("Email:", contact.email)
else:
print("Contact not found.")
elif choice == 4:
name = input("Enter name: ")
phone = input("Enter phone: ")
email = input("Enter email: ")
address_book.edit_contact(name, phone, email)
print("Contact updated.")
elif choice == 5:
address_book.display_contacts()
elif choice == 6:
break
else:
print("Invalid choice. Try again.")
if __name__ == '__main__':
main()
```
这个程序中,`Contact` 类表示一个通讯录条目,包含姓名、电话和电子邮件等信息。`AddressBook` 类表示整个通讯录,包含了添加、删除、查找、编辑和显示通讯录条目的方法。在程序运行时,会循环显示一个菜单,用户可以选择需要执行的操作。每个操作都会调用 `AddressBook` 类中对应的方法来完成相应的操作。
阅读全文
相关推荐
















