python 打印微信好友名字包含图片问题的解决方法

xiaoxiao2021-02-28  44

    刚学习python和itchat,发现通过itchat接口操作微信是非常方便,但在打印微信好友是碰到有些好友名称里包含图标时就会报各式各样的异常,如:UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001f527' in position 0: Non-BMP character not supported in Tk;我的解决方法是替换这些代表图标信息的字符,实例如下

import sysimport itchat

itchat.auto_login()users=itchat.get_friends()#生成一个字典non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)for user in users:      print(user['NickName'].translate(non_bmp_map))#用字典替换特殊字符,如代表图标信息的字符

转载请注明原文地址: https://www.6miu.com/read-2624243.html

最新回复(0)