python简单的好友通讯录管理程序怎么做

python简单的好友通讯录管理程序怎么做,第1张

friend={ '小明':['001', '广州'],'小红':['002','深圳'],'小王':['003','北京']}

model=input("model:")

if model=='1':

print("好友添加:",end='')

new_friend=input("newfriend:name,number,address")

friend_n=list(new_friend.split(','))

friend[friend_n[0]] =friend_n[1:3] # 添加

# friend4=dict.fromkeys([friend_n[0]],friend_n[1:3])

print("friend: ", friend)

elif model=='2':

print("好友删除:",end='')

new_friend = input("name")

# del friend[ new_friend] # 删除键是'Name'的条目

try:

friend.pop(new_friend)

print("friend: ", friend)

except:

print("查无此人")

elif model=='3':

print("好友修改:", end='')

new_friend = input("name")

friend[new_friend]=list(input("number,address").split(','))

print("friend: ", friend)

else:

print("好友查询:", end='')

new_friend = input("name")

try:

print(new_friend,':', friend[new_friend])

except:

print("查无此人")

以上程序包含了整个流程,有删赠改查功能,还有利用异常处理的处理过程。

1、首先,我们需要打开Python,建立一个程序。

2、其次该程序具有一个函数,该函数将名字和姓氏作为参数,并创建一个用户名。

3、最后使用sys.argv完成建立即可。

以列表的形式输出了好友的几项主要信息:uid,性别,屏幕名称和个人描述。

下面看一下getfriends.py的源码:

[python] view plain copy print?

#! /usr/bin/python

import time

PAGE_SIZE = 200

def print_users_list(ul):

"""

打印用户列表的详细信息

"""

index = 0

for user in ul:

uid = user["id"]

ugen = user["gender"]

uname = user["screen_name"]

# uloc = user["location"]

udesc = user["description"]

print "%-6d%-12d%-3s%s%s" % (index, uid, ugen, uname.ljust(20), udesc.ljust(40))

index += 1

def get_friends(client, uid=None, maxlen=0):

"""

读取uid用户的关注用户列表,默认uid=None,此时uid赋值为client.uid,而client.uid表示的是当前授权用户的uid.

"""

if not uid:

uid = client.uid

return get_users(client, False, uid, maxlen)

def get_followers(client, uid=None, maxlen=0):

"""

读取uid用户的粉丝列表,默认uid=None,此时uid赋值为client.uid,而client.uid表示的是当前授权用户的uid.

"""

if not uid:

uid = client.uid

return get_users(client, True, uid, maxlen)

def get_users(client, followersorfriends, uid, maxlen):

"""

调用API读取uid用户的关注用户列表或者粉丝列表,followersorfriends为True读取粉丝列表,为False读取关注好友列表

参数maxlen设置要获取的好友列表的最大长度,为0表示没有设置最大长度,此时会尝试读取整个好友列表,但是API对于读取的

好友列表的长度会有限制,测试等级最大只能获取一个用户的5000条好友信息。

"""

fl = []

next_cursor = 0

while True:

if followersorfriends:

raw_fl = client.friendships.followers.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)

else:

raw_fl = client.friendships.friends.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)

fl.extend(raw_fl["users"])

next_cursor = raw_fl["next_cursor"]

if not next_cursor:

break

if maxlen and len(fl) >= maxlen:

break

time.sleep(1)

return fl


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8107727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存